Javascript jquery确认密码验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4084975/
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
jquery confirm password validation
提问by Aryan G
I am using jquery for form validation. Rest is well except the confirm password field. Even when the same password is typed, the Please enter the same password.is not removed.
我正在使用 jquery 进行表单验证。除了确认密码字段外,其余都很好。即使输入相同的密码,请输入相同的密码。没有被移除。
My script is:
我的脚本是:
<script type="text/javascript">
$(document).ready(function() {
$("#form1").validate({
rules: {
password: {
required: true, minlength: 5
},
c_password: {
required: true, equalTo: "#password", minlength: 5
},
email: {
required: true, email: true
},
phone: {
required: true, number: true, minlength: 7
},
url: {
url: true
},
description: {
required: true
},
gender: {
required: true
}
},
messages: {
description: "Please enter a short description.",
gender: "Please select your gender."
}
});
});
-->
</script>
And inside the form tag:
在表单标签内:
<div class="form-row"><span class="label">Password</span><input type="password" name="password" class="required" id="password" /></div>
<div class="form-row"><span class="label">Confirm Password</span><input type="password" name="c_password" id="c_password" /></div>
Any suggestion? Would be much thankful for the help.
有什么建议吗?将非常感谢您的帮助。
采纳答案by MatuDuke
Your fields doesn't have the ID property.
In jQuery the "#password" selector means "the object that has an id
property with value 'password'
"
您的字段没有 ID 属性。在 jQuery 中,“#password”选择器的意思是“id
具有值的属性的对象'password'
”
Your code should look like this:
您的代码应如下所示:
<input type="password" name="password" id="password" class="required"/>
回答by Thiwanka
<input type="password" id="password" class="nomal required error" value="" name="cpassword">
<input type="password" equalto="#password" class="nomal required error" value="" name="cpassword">
回答by Craig
rules: {
isn't closed. Put another }
after:
没有关闭。再放一个}
:
messages: {
description: "Please enter a short description.",
gender: "Please select yor gender."
}
This is as well as the answer about the element ID's.
这也是关于元素 ID 的答案。
回答by Dalen
Be sure that your password's input text has id 'password'
确保您的密码输入文本具有 id 'password'
回答by Pat
Be sure that no other input with tag id='password'
in that same page.
确保id='password'
在同一页面中没有其他带有标签的输入。
回答by sgimeno
I came across some issues when using camelCase id's for the password inputs. I finnally got it working with different 'name' and 'id'.
我在使用驼峰命名法输入密码时遇到了一些问题。我终于让它使用了不同的“名称”和“ID”。
<input type="password" id="pass" placeholder="New password" name="newPassword">
<input type="password" id="pass2" placeholder="New password" name="repeatNewPassword">
<input type="email" id="inputNewEmail" name="newEmail" placeholder="New email">
<input type="email" id="repeatNewEmail" name="repeatNewEmail" placeholder="New email>
Then in the JS:
然后在JS中:
rules: {
newPassword: {
minlength: 6
},
repeatNewPassword: {
minlength: 6,
equalTo: "#pass"
},
newEmail: { email: true},
repeatNewEmail: {email: true, equalTo: '#inputNewEmail'},
}
So refer to the input field by its 'name' but define equalTo deppendency using 'id'. I'm not sure about the camelCase issue, for the email inputs it worked, but exactly same nomenclature with password inputs didn't work, according to my experience
因此,通过其“名称”引用输入字段,但使用“id”定义 equalTo 依赖关系。根据我的经验,我不确定camelCase问题,对于电子邮件输入它起作用,但与密码输入完全相同的命名法不起作用
回答by ranjan kumar
if($("#newpassword").val()!== ($("#conformpassword").val())){
$('#newpasswordId').html('<font color="red">Your password does not match</font>');
$("#newpassword").val('');
$("#conformpassword").val('');
$('#newpassword').css("border", "#FF0000 solid 1px")
$('#conformpassword').css("border", "#FF0000 solid 1px")
$("#newpassword").focus();
return false;
}