jquery 表单验证插件:字段 1 不等于字段 2

发布于 2024-12-12 07:35:49 字数 1624 浏览 0 评论 0原文

嘿嘿,

我遇到了以下问题:我尝试阻止用户为“用户名”和“电子邮件”字段选择相同的值。我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如若梦似彩虹 2024-12-19 07:35:49

您可以添加检查用户名和电子邮件值的自定义方法,并为该字段添加验证。

演示 - http://jsfiddle.net/2hA8M/8/

示例 -

方法 -

$.validator.addMethod("user_email_not_same", function(value, element) {
   return $('#name').val() != $('#email').val()
}, "* User and Email should not match");

验证 -

rules : {
     username : {
         user_email_not_same: true
     }
 },

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 -

$.validator.addMethod("user_email_not_same", function(value, element) {
   return $('#name').val() != $('#email').val()
}, "* User and Email should not match");

Validation -

rules : {
     username : {
         user_email_not_same: true
     }
 },
美胚控场 2024-12-19 07:35:49

这可能更灵活:

$.validator.addMethod("notEqualTo", function (value, element, param)
{
    var target = $(param);
    if (value) return value != target.val();
    else return this.optional(element);
}, "Does not match");

调用:

rules: {
    username: {
        required: true,
        minlength: 2,
        remote: "/_misc/formcheck/username.cfm",
        notEqualTo: "#emailId-or-anyValidJquerySelector"
    }
}

示例:http://jsfiddle.net/4AdWR/1/

或者直接在 Html 中:

<input type="text" name="username" notEqualTo="#emailId-or-anyValidJquerySelector" minlength="2" class="required" />

示例: http://jsfiddle.net/B2hWn/1/

这也尊重您需要/可选的先前验证;)

This could be more flexible:

$.validator.addMethod("notEqualTo", function (value, element, param)
{
    var target = $(param);
    if (value) return value != target.val();
    else return this.optional(element);
}, "Does not match");

The call:

rules: {
    username: {
        required: true,
        minlength: 2,
        remote: "/_misc/formcheck/username.cfm",
        notEqualTo: "#emailId-or-anyValidJquerySelector"
    }
}

Sample: http://jsfiddle.net/4AdWR/1/

Or directly in Html:

<input type="text" name="username" notEqualTo="#emailId-or-anyValidJquerySelector" minlength="2" class="required" />

Sample: http://jsfiddle.net/B2hWn/1/

And this also respects you required/optional previous validations ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文