繁体   English   中英

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

[英]Limit number of characters in input field with PHP

我正在使用此WordPress插件创建人员数据库。 在注册表单上,我想限制用户可以在某些字段中输入的字符数,但是我不知道怎么做,因为我不是程序员,并且对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>

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

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

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

像这样使用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     
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM