使用 jQuery Validate 确认密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14703374/
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
Confirm Password with jQuery Validate
提问by ldrocks
I'm trying to use jQuery validate plugin to confirm my password entry.
我正在尝试使用 jQuery 验证插件来确认我的密码输入。
However, I don't want it to be a required field.
但是,我不希望它成为必填字段。
Just IF a user wants to change the password, they would need to confirm it.
只要用户想要更改密码,他们就需要确认。
If not, both fields shouldn't be validated.
如果不是,则不应验证两个字段。
jQuery('.validatedForm').validate({
rules: {
password: {
required: true,
minlength: 5
},
password_confirm: {
required: true,
minlength: 5,
equalTo: "#password"
}
}
});
This is working perfectly, but again, both fields are required.
这工作得很好,但同样,这两个字段都是必需的。
I would like to have this optional, in case someone just wants to change for instance their email or username and leave the password alone.
我想要这个可选的,以防有人只想更改例如他们的电子邮件或用户名而不要管密码。
回答by Arun P Johny
回答by Half Crazed
Just a quick chime in here to hopefully help others... Especially with the newer version (since this is 2 years old)...
只是在这里快速提示一下,希望能帮助其他人......尤其是新版本(因为这是 2 岁)......
Instead of having some static fields defined in JS, you can also use the data-rule-*
attributes. You can use built-in rules as well as custom rules.
除了在 JS 中定义一些静态字段之外,您还可以使用data-rule-*
属性。您可以使用内置规则以及自定义规则。
See http://jqueryvalidation.org/documentation/#link-list-of-built-in-validation-methodsfor built-in rules.
有关内置规则,请参阅http://jqueryvalidation.org/documentation/#link-list-of-built-in-validation-methods。
Example:
例子:
<p><label>Email: <input type="text" name="email" id="email" data-rule-email="true" required></label></p>
<p><label>Confirm Email: <input type="text" name="email" id="email_confirm" data-rule-email="true" data-rule-equalTo="#email" required></label></p>
Note the data-rule-*
attributes.
注意data-rule-*
属性。
回答by Krishna Kumar
It works if id value and name value are different:
如果 id 值和 name 值不同,它会起作用:
<input type="password" class="form-control"name="password" id="mainpassword">
password: { required: true, } ,
cpassword: {required: true, equalTo: '#mainpassword' },
回答by Melis
I'm implementing it in Play Framework and for me it worked like this:
我在 Play Framework 中实现它,对我来说它是这样工作的:
1) Notice that I used data-rule-equalTo in input tag for the id inputPassword1. The code section of userform in my Modal:
1) 请注意,我在 id inputPassword1 的输入标签中使用了 data-rule-equalTo。我的模态中用户表单的代码部分:
<div class="form-group">
<label for="pass1">@Messages("authentication.password")</label>
<input class="form-control required" id="inputPassword1" placeholder="@Messages("authentication.password")" type="password" name="password" maxlength=10 minlength=5>
</div>
<div class="form-group">
<label for="pass2">@Messages("authentication.password2")</label>
<input class="form-control required" data-rule-equalTo="#inputPassword1" id="inputPassword2" placeholder="@Messages("authentication.password")" type="password" name="password2">
</div>
2)Since I used validator within a Modal
2)因为我在模态中使用了验证器
$(document).on("click", ".createUserModal", function () {
$(this).find('#userform').validate({
rules: {
firstName: "required",
lastName: "required",
nationalId: {
required: true,
digits:true
},
email: {
required: true,
email: true
},
optradio: "required",
password :{
required: true,
minlength: 5
},
password2: {
required: true
}
},
highlight: function (element) {
$(element).parent().addClass('error')
},
unhighlight: function (element) {
$(element).parent().removeClass('error')
},
onsubmit: true
});
});
Hope it helps someone :).
希望它可以帮助某人:)。
回答by PhonPanom
jQuery('.validatedForm').validate({
rules : {
password : {
minlength : 5
},
password_confirm : {
minlength : 5,
equalTo : '[name="password"]'
}
}
In general, you will not use id="password"
like this.
So, you can use [name="password"]
instead of "#password"
一般来说,你不会这样使用id="password"
。因此,您可以使用[name="password"]
代替"#password"