jQuery 确认密码的jquery验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24139578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 02:28:41  来源:igfitidea点击:

Jquery validation for confirm password

jqueryjquery-validate

提问by user3599482

I am using Jquery for form validation , in that I have password and confirm password fields like this,

我使用 Jquery 进行表单验证,因为我有密码和确认密码字段,如下所示,

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

Here other fields are working fine but not confirm password field, The rule for cfmPasswordis as follow ,

这里其他字段工作正常但不能确认密码字段,规则cfmPassword如下,

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

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

but here the problem is if cfmpasswordis not same as passwordalso it doesnt show error message. please any one help me in this.

但这里的问题是如果cfmpassword不一样password也不显示错误消息。请任何人帮助我。

回答by faby

working fiddle 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"

               }
     }

});

回答by Mr7-itsurdeveloper

There was syntax error

有语法错误

just added colon in password: after maxlength: 10,

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

your code working fine.now

你的代码工作正常。现在

<script>

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

     } , 

     cfmPassword: { 

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


  }

});
</script>