我正在使用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
}
但问题是如果cfmpassword
与password
不同,它也不会显示错误消息。请任何人帮助我。
答案 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>