1

I'm creating a database of people using this WordPress Plugin. On the signup form, I want to limit the number of characters the users can input in certain fields, but I don't know how, as I'm not a programmer and have limited knowledge of PHP and Javascript. I can't edit the HTML directly since I'm using a plugin, so I can't just use the maxlength HTML attribute. The signup page is based on the following PHP template from the plugin, so I guess that's what I need to edit, but how?

<?php
/*
 * bootstrap template for signup form
 *
 * outputs a Twitter Bootstrap-compatible form
 * http://twitter.github.com/bootstrap/index.html
 *
*/
?>

<div class="wrap <?php echo $this->wrap_class ?>" >

  <?php // this is how the html wrapper for the error messages can be customized
  $this->print_errors( '<div class="alert %1$s">%2$s</div>','<p>%s</p>' ); ?>

  <?php $this->print_form_head(); // this must be included before any fields are output ?>

    <div class="form-horizontal pdb-signup">

     <?php while ( $this->have_groups() ) : $this->the_group(); ?>

        <fieldset class="field-group field-group-<?php echo $this->group->name ?>">
                <?php $this->group->print_title( '<legend>', '</legend>' ) ?>
                <?php $this->group->print_description() ?>

        <?php while ( $this->have_fields() ) : $this->the_field(); ?>

       <?php $feedback_class = $this->field->has_error() ? 'error' : ''; ?>

       <div class="<?php $this->field->print_element_class() ?> control-group <?php echo $feedback_class ?>">

          <label class="control-label" for="<?php $this->field->print_element_id() ?>" ><?php $this->field->print_label(); // this function adds the required marker ?></label>
          <div class="controls"><?php $this->field->print_element_with_id(); ?>

                        <?php if ( $this->field->has_help_text() ) :?>
             <span class="help-block">
                <?php $this->field->print_help_text() ?>
             </span>
            <?php endif ?>

          </div>

        </div>

       <?php endwhile; // fields ?>

        </fieldset>

      <?php endwhile; // groups ?>
      <fieldset class="field-group field-group-submit">
       <div id="submit-button" class="controls">
          <?php $this->print_submit_button('btn btn-primary'); // you can specify a class for the button ?>
          <span class="pdb-retrieve-link"><?php $this->print_retrieve_link(__('Forget your private link? Click here to have it emailed to you.','participants-database')); ?></span>
        </div>
      </fieldset>
    </div>
  <?php $this->print_form_close() ?>
</div>
2
  • 1
    why not just use size attribute in html? Commented Jul 27, 2015 at 11:36
  • I can't access the HTML directly. I'm using a plugin that runs a PHP template to generate the page.
    – ReddaJoppe
    Commented Jul 27, 2015 at 19:44

2 Answers 2

1

Find out id of your input field and add following script into your file.

<script type="text/javascript">   
    window.onload = function() {

        jQuery('#input_field_id').attr('maxlength','100');
    }
</script>
3
  • Doesn't seem to work. Where exactly should I put that code, and what is supposed to happen when a user exceeds the character limit on the field in question and clicks 'submit'?
    – ReddaJoppe
    Commented Jul 27, 2015 at 19:43
  • You can put it in your header.php file or in above mentioned file (signup). The maxlenght property not allowed to exceed than given limit. You an also use as. jQuery('#input_field_id').prop('maxlength','100');
    – Tushar
    Commented Jul 28, 2015 at 12:14
  • And If you are using Jquery in your project then you can also use following code $(document).ready(function(){ $('#id_field_name').attr('maxlength','3'); or $('#id_field_name').prop('maxlength','3'); }); Demo: jsfiddle.net/2cab3g2o
    – Tushar
    Commented Jul 28, 2015 at 12:25
0

Use strlen() Like this:

<?php
 session_start();

 $Text = html_specialchars($_POST['Input_1']);
 $Text_max = 100;   //characters for max input


 if(strlen($Text)>$Text_max){
     //$Text has more than 100 characters (including spaces!)
 }


 else{
     //Do your thing     
 }
3
  • Where does that code go exactly, and what do I change to control which field it is applied to?
    – ReddaJoppe
    Commented Jul 27, 2015 at 19:45
  • @JeppeBech , do you have the page for where the information is sent? ALSO if you are using mysql, you can limit the maximum of characters that the database will store, say you set it to 150 then it will only store the first 150 characters. Altough, if you can explain me the fields you need i could help you out with the code :)
    – MrK
    Commented Jul 27, 2015 at 20:50
  • This is the signup page. For instance, I would like to limit the number of characters the user can type in the field 'Overskrift' to 140.
    – ReddaJoppe
    Commented Jul 28, 2015 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.