jQueryvalidation多个不相等的输入

我已经设法在我的表单上设置了jQuery validate插件,并为两个字段提供了规则,其中的值不应该匹配。 具体来说,电子邮件和email_2输入现在不能相同,这是有效的。 但我真正需要的是以相同的方式validation多个输入(在这种情况下为4个)。 我有电子邮件,email_2,email_3,email_4,但这些都不应该是平等的。 你可以在我的jsfiddle中看到它,如果你有解决方案,你可以更新它并把它放回答案中:

HTML:





jQuery的:

 $.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 }, }, }); 

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

是的,它仍适用于1.10.1

DEMO

来源: http : //www.anujgakhar.com/2010/05/24/jquery-validator-plugin-and-notequalto-rule-with-multiple-fields/

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'] }, }, }); 

你可以使用$ .unique():

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