确认密码的Jquery验证

时间:2014-06-10 11:13:18

标签: jquery jquery-validate

我正在使用Jquery进行表单验证,因为我有密码并确认密码字段,如下,

<input type="password" class="form-control" name="password" id="password">
<input type="password" class="form-control" name="cfmPassword" id="cfmPassword" >

此处其他字段工作正常但未确认密码字段, cfmPassword的规则如下,

password: {
    required: true,
    minlength: 6,
    maxlength: 10

},
cfmPassword: {
    required: true,
    equalTo: "#password",
    minlength: 6,
    maxlength: 10
}

但问题是如果cfmpasswordpassword不同,它也不会显示错误消息。请任何人帮助我。

2 个答案:

答案 0 :(得分:14)

工作小提琴here

<form id="formCheckPassword">
    <input type="password" class="form-control" name="password" id="password"/>
    <input type="password" class="form-control" name="cfmPassword" id="cfmPassword" />
    <input type="submit" value="submit"/>
 </form>



 $("#formCheckPassword").validate({
           rules: {
               password: { 
                 required: true,
                    minlength: 6,
                    maxlength: 10,

               } , 

                   cfmPassword: { 
                    equalTo: "#password",
                     minlength: 6,
                     maxlength: 10
               }


           },
     messages:{
         password: { 
                 required:"the password is required"

               }
     }

});

答案 1 :(得分:1)

存在语法错误

刚刚在密码中添加了冒号:在maxlength:10之后,

你的代码工作正常。现在

<script>

  $("#Myform").validate({
   rules: {
     password: { 
       required: true,
       minlength: 6,
       maxlength: 10,

     } , 

     cfmPassword: { 

      equalTo: "#password",
      minlength: 6,
      maxlength: 10
    }


  }

});
</script>