jquery 表单验证插件:字段 1 不等于字段 2
嘿嘿,
我遇到了以下问题:我尝试阻止用户为“用户名”和“电子邮件”字段选择相同的值。我正在使用 jquery 表单验证插件(http://bassistance.de/jquery-plugins/jquery-plugin-validation/),
如果有人能给我指路,那就太棒了,请在这里查看我的代码:
// validate form
$("#signupform").validate({
rules: {
username: {
required: true,
minlength: 2,
remote: "/_misc/formcheck/username.cfm"
},
password1: {
required: true,
minlength: 5
},
password2: {
required: true,
minlength: 5,
equalTo: "#password1"
},
email: {
required: true,
email: true,
remote: "/_misc/formcheck/email.cfm"
},
email2: {
required: true,
email: true,
equalTo: "#email"
},
usage: "required"
},
messages: {
username: {
required: "please username!!",
minlength: "username at least 2 chars"
},
password1: {
required: "please password!",
minlength: "password at least 5 chars"
},
password2: {
required: "please password",
minlength: "password at least 5 chars",
equalTo: "password fields have to match"
},
email: "validate email please!",
email2: {
required: "please provide correct email address!",
equalTo: "email fields must match!"
},
usage: "please check my terms!"
},
提前致谢,米赫贝克
heyho,
i stuck in the following problem: i try to prevent that a user can choose for the fields "username" and "email" the same values. i'm using the jquery form validate plugin (http://bassistance.de/jquery-plugins/jquery-plugin-validation/)
would be awesome if somebody could show me the way, please see my code here:
// validate form
$("#signupform").validate({
rules: {
username: {
required: true,
minlength: 2,
remote: "/_misc/formcheck/username.cfm"
},
password1: {
required: true,
minlength: 5
},
password2: {
required: true,
minlength: 5,
equalTo: "#password1"
},
email: {
required: true,
email: true,
remote: "/_misc/formcheck/email.cfm"
},
email2: {
required: true,
email: true,
equalTo: "#email"
},
usage: "required"
},
messages: {
username: {
required: "please username!!",
minlength: "username at least 2 chars"
},
password1: {
required: "please password!",
minlength: "password at least 5 chars"
},
password2: {
required: "please password",
minlength: "password at least 5 chars",
equalTo: "password fields have to match"
},
email: "validate email please!",
email2: {
required: "please provide correct email address!",
equalTo: "email fields must match!"
},
usage: "please check my terms!"
},
thanks in advance, michbeck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以添加检查用户名和电子邮件值的自定义方法,并为该字段添加验证。
演示 - http://jsfiddle.net/2hA8M/8/
示例 -
方法 -
验证 -
You can add a custom method checking the user name and email values and add validation for that field.
Demo - http://jsfiddle.net/2hA8M/8/
Example -
Method -
Validation -
这可能更灵活:
调用:
示例:http://jsfiddle.net/4AdWR/1/
或者直接在 Html 中:
示例: http://jsfiddle.net/B2hWn/1/
这也尊重您需要/可选的先前验证;)
This could be more flexible:
The call:
Sample: http://jsfiddle.net/4AdWR/1/
Or directly in Html:
Sample: http://jsfiddle.net/B2hWn/1/
And this also respects you required/optional previous validations ;)