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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7905368/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:25:48  来源:igfitidea点击:

jquery form validate plugin: field 1 not equal to field 2

jqueryjquery-plugins

提问by michbeck

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/)

我遇到了以下问题:我试图防止用户可以为“用户名”和“电子邮件”字段选择相同的值。我正在使用 jquery 表单验证插件(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

提前致谢,米贝克

回答by Jayendra

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/

演示 - 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
     }
 },

回答by kpull1

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/

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

Or directly in Html:

或者直接在 Html 中:

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

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

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

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

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