Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多个不相等输入的jQuery验证_Jquery_Validation_Email_Input - Fatal编程技术网

多个不相等输入的jQuery验证

多个不相等输入的jQuery验证,jquery,validation,email,input,Jquery,Validation,Email,Input,我已经在我的表单上设置了jqueryvalidate插件,并为两个字段提供了规则,在这两个字段中,它们的值不应该匹配。具体来说,电子邮件和电子邮件输入现在不能相同,这是可行的。但我真正需要的是以相同的方式验证多个输入(在本例中为4个)。我有email,email_2,email_3,email_4,它们都不应该是相等的。您可以在my中看到它,如果您有解决方案,您可以更新它并将其放回答案中: html: 验证所有输入的最佳解决方案是什么?您可以使用$.unique() uniqArray=$.un

我已经在我的表单上设置了jqueryvalidate插件,并为两个字段提供了规则,在这两个字段中,它们的值不应该匹配。具体来说,电子邮件和电子邮件输入现在不能相同,这是可行的。但我真正需要的是以相同的方式验证多个输入(在本例中为4个)。我有email,email_2,email_3,email_4,它们都不应该是相等的。您可以在my中看到它,如果您有解决方案,您可以更新它并将其放回答案中:

html:

验证所有输入的最佳解决方案是什么?

您可以使用$.unique()

uniqArray=$.unique(emailsArray);
if(uniqArray.length!=emailsArray.length){
}

是的,它仍然适用于1.10.1

资料来源:

HTML



jQuery

jQuery.validator.addMethod("notEqualToGroup", function (value, element, options) {
    // get all the elements passed here with the same class
    var elems = $(element).parents('form').find(options[0]);
    // the value of the current element
    var valueToCompare = value;
    // count
    var matchesFound = 0;
    // loop each element and compare its value with the current value
    // and increase the count every time we find one
    jQuery.each(elems, function () {
        thisVal = $(this).val();
        if (thisVal == valueToCompare) {
            matchesFound++;
        }
    });
    // count should be either 0 or 1 max
    if (this.optional(element) || matchesFound <= 1) {
        //elems.removeClass('error');
        return true;
    } else {
        //elems.addClass('error');
    }
}, jQuery.format("Please enter a Unique Value."))

// validate form    
$("#signupform").validate({

    rules: {
        email: {
            required: true,
            notEqualToGroup: ['.distinctemails']
        },
        email_2: {
            required: true,
            notEqualToGroup: ['.distinctemails']
        },
        email_3: {
            required: true,
            notEqualToGroup: ['.distinctemails']
        },
        email_4: {
            required: true,
            notEqualToGroup: ['.distinctemails']
        },
    },
});
jQuery.validator.addMethod(“notEqualToGroup”,函数(值、元素、选项){
//使用同一个类获取此处传递的所有元素
var elems=$(元素).parents('form').find(选项[0]);
//当前元素的值
var valueToCompare=价值;
//计数
var matchesFound=0;
//循环每个元素并将其值与当前值进行比较
//每次我们找到一个就增加计数
每个(元素、函数(){
thisVal=$(this.val();
if(thisVal==valueToCompare){
matchesFound++;
}
});
//计数最多应为0或1
if(this.optional(element)| | matchesFound
uniqArray=emailsArray.slice();
$.unique(uniqArray);
if(uniqArray.length!=emailsArray.length){
}

检查这个:谢谢,但是,我试着这样做,但我在jQuery库中遇到了错误,似乎这在最新的1.10.1上不起作用……我不太清楚如何做,最后,我是jQuery nooby。你能在我的JSFIDLE中给我演示一下吗?拜托?实际上,kei的解决方案要好得多。我不知道“验证器”我想我在其他地方错过了一个错误。我想当控制台给我库错误时,它是不受支持的。如果我有一系列电子邮件,比如email[]而不是email\u 1,email\u 2。。
$.validator.addMethod("notequal", function(value, element) {
 return $('#email').val() != $('#email_2').val()}, 
 "* You've entered this one already");

// validate form    
$("#signupform").validate({

rules: {
    email: {
        required: true,
        notequal: true
    },
    email_2: {
        required: true,
        notequal: true
    },
},
});
uniqArray = $.unique(emailsArray);
if (uniqArray.length != emailsArray.length) {
    <your magic here>
}
<form id="signupform" method="post">
    <p>
        <input class="distinctemails" id="email" name="username" /></p>
    <p>
        <input class="distinctemails" id="email_2" name="email_2" /></p>
    <p>
        <input class="distinctemails" id="email_3" name="email_3" /></p>
    <p>
        <input class="distinctemails" id="email_4" name="email_4" /></p>
    <p>
        <input id="submit" type="submit" value="submit" />
    </p>
</form>
jQuery.validator.addMethod("notEqualToGroup", function (value, element, options) {
    // get all the elements passed here with the same class
    var elems = $(element).parents('form').find(options[0]);
    // the value of the current element
    var valueToCompare = value;
    // count
    var matchesFound = 0;
    // loop each element and compare its value with the current value
    // and increase the count every time we find one
    jQuery.each(elems, function () {
        thisVal = $(this).val();
        if (thisVal == valueToCompare) {
            matchesFound++;
        }
    });
    // count should be either 0 or 1 max
    if (this.optional(element) || matchesFound <= 1) {
        //elems.removeClass('error');
        return true;
    } else {
        //elems.addClass('error');
    }
}, jQuery.format("Please enter a Unique Value."))

// validate form    
$("#signupform").validate({

    rules: {
        email: {
            required: true,
            notEqualToGroup: ['.distinctemails']
        },
        email_2: {
            required: true,
            notEqualToGroup: ['.distinctemails']
        },
        email_3: {
            required: true,
            notEqualToGroup: ['.distinctemails']
        },
        email_4: {
            required: true,
            notEqualToGroup: ['.distinctemails']
        },
    },
});
uniqArray = emailsArray.slice();

$.unique(uniqArray);

if (uniqArray.length != emailsArray.length) {
    <your magic here>
}