I have six input fields in form and I need to do some validation to make sure that none of them are identical in real time. For example if a user try's to enter a duplicate field jQuery would prompt the user that they must enter unique fields, before even submitting the form. I assume that I could use a 'keyup' function but I'm not sure. I'm trying to use jQuery validate but can only get it working for two fields at a time. Is this possible with jQuery or maybe there is a better solution? Angular?
2 Answers
Give all the fields a class like class="alldifferent". Then you can use:
$(".alldifferent").keyup(function() {
var val = $(this).val();
if (val != '') {
$(".alldifferent").not(this)).each(function() {
if ($(this).val() == val) {
alert("Error, duplicate value " + val);
return false; // stop the loop
}
});
}
});
1 Comment
webDevleoper101
Thank You! exactly what I was looking for.
The Jquery Validation work on submit from you can use keyup function see in documentation
1 Comment
Barmar
This seems to be the opposite of what he wants.