Last active
June 20, 2019 13:34
-
-
Save NimzyMaina/b97852a5adee45334b25503965f1b68f to your computer and use it in GitHub Desktop.
Jquery validation unique from other input (use class to differentiate)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is used by jquery validator to ensure unique fields in a form
Simply add a class to the input field to differentiate them. Then in the validation rules add this