【问题标题】:JQuery validation accepts even if input contains only whitespaces即使输入仅包含空格,JQuery 验证也接受
【发布时间】:2017-02-14 15:01:55
【问题描述】:

我无法验证输入字段是否仅包含空格。我已经为必填字段指定了必填规则,但如果我只输入空格,它仍然有效。此外,用户名字段的 noSpace 规则似乎不起作用。它禁用其他字段的验证。这是我的标记。

<form method=post class="form-auth-small" action="../php/controller/registration_controller.php">
  <div class="form-group row m-t-10px">
    <div class="col-sm-6 text-left m-b-5px">
      <label><h5>RANK: </h5></label>
      <select class="form-control" name="rank" required="required">
        <option value="rank1">NUP</option>
        <option value="rank2">PO1</option>
        <option value="rank3">PO2</option>
        <option value="rank4">PO3</option>
        <option value="rank5">SPO1</option>
        <option value="rank6">SPO2</option>
        <option value="rank7">SPO3</option>
        <option value="rank8">SPO4</option>
        <option value="rank9">PINSP</option>
        <option value="rank10">PSINSP</option>
        <option value="rank11">PCINSP</option>
        <option value="rank12">PSUPT</option>
        <option value="rank13">PSSUPT</option>
        <option value="rank14">PCSUPT</option>
        <option value="rank15">PDIR</option>
        <option value="rank16">PDDG</option>
        <option value="rank17">PDG</option>
      </select>
    </div>
    <div class="col-sm-6 text-left m-b-5px">
      <label><h5>USERNAME: </h5></label>
      <input class="form-control" name="username" placeholder="Username must only contain characters [A-Z] and numbers [0-9]" type="text" minlength="1" required="required">
    </div>
    <div class="clearfix"></div>
    <div class="col-sm-6 text-left m-b-5px">
      <label><h5>FIRST NAME: </h5></label>
      <input class="form-control" name="fname" type="text" minlength="1" required="required">
    </div>
    <div class="col-sm-6 text-left m-b-5px">
      <label><h5>PASSWORD: </h5></label>
      <input class="form-control" name="pwd" placeholder="Choose a strong password" type="password" minlength="1" required="required">
    </div>
    <div class="clearfix"></div>
    <div class="col-sm-6 text-left m-b-5px">
      <label><h5>MIDDLE NAME: </h5></label>
      <input class="form-control" name="mname" type="text" minlength="1" required="required">
    </div>
    <div class="col-sm-6 text-left m-b-5px">
      <label><h5>EMAIL: </h5></label>
      <input class="form-control" name="email" type="email" minlength="1" required="required">
    </div>
    <div class="clearfix"></div>
    <div class="col-sm-6 text-left m-b-5px">
      <label><h5>LAST NAME: </h5></label>
      <input class="form-control" name="lname" type="text" minlength="1" required="required">
    </div>
  </div>
  <div class="form-group clearfix m-b-5px">
    <button type="submit" name="btnRegister" class="btn btn-primary btn-lg btn-block">REGISTER</button>
  </div>
  <div class="bottom m-b-15px">
    <span><i class="fa fa-lock"></i> <a href="login.php">Already have an account? Sign in.</a></span>
  </div>
</form>

JS(没有 noSpace 规则)

$(document).ready(function () {
$('.form-auth-small').validate({
  rules: { 
    rank: {
      required: true
    },
    username: {
      required: true,
      minlength: 3
    },
    fname: {
      required: true
    },
    mname: {
      required: true
    },
    lname: {
      required: true
    },
    pwd: {
      required: true
    },
    email: {
      required: true
    }
  },
  errorElement: "em",
  errorPlacement: function ( error, element ) {
    // Add the `help-block` class to the error element
    error.addClass( "help-block" );

    // Add `has-feedback` class to the parent div.form-group
    // in order to add icons to inputs
    element.parents( ".col-sm-5" ).addClass( "has-feedback" );

    error.insertAfter( element );

    // Add the span element, if doesn't exists, and apply the icon classes to it.
    if ( !element.next( "span" )[ 0 ] ) {
      $( "<span class='glyphicon glyphicon-remove form-control-feedback'></span>" ).insertAfter( element );
    }
  },
  success: function ( label, element ) {
    // Add the span element, if doesn't exists, and apply the icon classes to it.
    if ( !$( element ).next( "span" )[ 0 ] ) {
      $( "<span class='glyphicon glyphicon-ok form-control-feedback'></span>" ).insertAfter( $( element ) );
    }
  },
  highlight: function ( element, errorClass, validClass ) {
    $( element ).parents( ".col-sm-6" ).addClass( "has-error" ).removeClass( "has-success" );
    $( element ).next( "span" ).addClass( "glyphicon-remove" ).removeClass( "glyphicon-ok" );
  },
  unhighlight: function ( element, errorClass, validClass ) {
    $( element ).parents( ".col-sm-6" ).addClass( "has-success" ).removeClass( "has-error" );
    $( element ).next( "span" ).addClass( "glyphicon-ok" ).removeClass( "glyphicon-remove" );
  }
});
});

DEMO 提前谢谢!

【问题讨论】:

  • 你试过noSpace: true吗?
  • @Tumen_t 是的,我有。除非我的实现是错误的。当我在 minlength: 3 字段(以逗号分隔)下方添加它时,它禁用了对其他字段的验证。
  • trimming 该字段中的值怎么样.. 如果它仍然是空白.. 那么会发生什么?
  • @BviLLe_Kid 我不认为我应该这样做。

标签: javascript jquery html validation


【解决方案1】:

在你的 jQuery 验证函数运行之前添加这个方法::

jQuery.validator.addMethod("noSpace", function(value, element) { 
    return value.indexOf(" ") < 0 && value != ""; 
  }, "No space please and don't leave it empty");

使用

noSpace: true

对于您想要的每个字段。

【讨论】:

  • 我会试试这个。
  • 同时尝试这两种方法。
  • 好的,我会的。谢谢!
  • 我让它工作了,谢谢!我不得不做这两种方法。自从您第一次发布以来,我接受了您的回答。
  • 我可以问一下吗?如何让它只在开头和结尾不允许空格,而不是在字符之间?
【解决方案2】:

试试这个

$(document).ready(function() {
  jQuery.validator.addMethod("noSpace", function(value, element) { 
     return value.indexOf(" ") < 0 && value != ""; 
}, "No space");


$(".form-auth-small").validate({
   rules: {
      username : {
          noSpace: true
      }
   }
  });


})

【讨论】:

  • 看看这个演示。它几乎与您所拥有的相同。 jsbin.com/akero3/52/edit?html,js,output
  • 谢谢。但这和拉娜的回答是一样的。我不得不接受他的回答,因为他两分钟前发帖了。
【解决方案3】:
$(".form-auth-small").validate({
   rules: {
      username : {
          required: true,
          normalizer: function(value) {
                return $.trim(value);
            }
      }
   }
  });

【讨论】:

  • 请添加一些解释,即使代码是解释性的。
猜你喜欢
  • 2020-05-11
  • 1970-01-01
  • 2021-05-12
  • 1970-01-01
  • 2021-03-19
  • 1970-01-01
  • 2017-08-17
  • 2013-08-31
  • 1970-01-01
相关资源
最近更新 更多