I've improved on your version - when a duplication is detected, any one of the duplicated fields should be marked as invalid, and should be able to be changed in order to fix the problem. So, you'd need to revalidate all other fields every time (using validator.element()
), but avoid a recursion (using validator.validatingOthers
).
I'll post the code for checkMachineIDs here for completeness:
function checkMachineIDs(element){
if($(element).val() != ""){
var arrElements = $("#machineList .machineID");
var $element = $(element);
var validator = $($element[0].form).validate();
if(arrElements.length > 1){
var valid = true;
arrElements.not('#'+$element.attr('id')).each(function() {
var current = $(this);
if (current.val() == $element.val())
valid = false;
});
if (!validator.validatingOthers) {
validator.validatingOthers = true;
arrElements.not('#'+$element.attr('id')).each(function() {
validator.element(this);
});
if (valid) validator.element($element);
validator.validatingOthers = false;
}
return valid;
}else{
return true;
}
}else{
return true;
}
}