I have a form with a dozen email input fields and each email needs to be unique. What is the most elegant way via jquery to perform the comparison among all the fields without manually writing all the comparisons like below:
if($("#email1").val() == $("#email2").val() || ....) {
isValid = false;
alert("emails must be unique.");
}
Thanks in advance!
The simplest way is to put the emails into a
Set, and compare its size:If you can’t use ES6 (or other modern JS features), use jQuery’s inArray:
If that selector is too broad, you can specify your IDs in a comma separated list, like this:
or just add a class to all the elements you want to select…