使用PHP限制输入字段中的字符数

时间:2015-07-27 11:34:41

标签: javascript php wordpress wordpress-plugin maxlength

我正在使用this WordPress Plugin创建人员数据库。在注册表单上,我想限制用户可以在某些字段中输入的字符数,但我不知道如何,因为我不是程序员,并且对PHP和Javascript的知识有限。 我无法直接编辑HTML,因为我使用的是插件,所以我不能只使用maxlength HTML属性。 注册页面基于插件中的以下PHP模板,所以我想这就是我需要编辑的内容,但是如何编辑?

<?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 个答案:

答案 0 :(得分:1)

找出输入字段的ID并将以下脚本添加到您的文件中。

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

        jQuery('#input_field_id').attr('maxlength','100');
    }
</script>

答案 1 :(得分:0)

使用strlen()像这样:

<?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     
 }