The easiest way is to put emails in Set
and compare its size:
const $inputs = $('input[id^=email]'); const uniques = new Set($input.map((i, el) => el.value).get()); if (uniques.size < $inputs.length) { alert('Emails must be unique.'); }
If you cannot use ES6 (or other modern JS functions), use jQuery inArray :
var values = []; $('input[id^=email]').each(function () { if ($.inArray(this.value, values) >= 0) { alert('Emails must be unique.'); return false;
If this selector is too wide, you can specify your identifiers in a comma-separated list, for example:
$('#email1, #email2, #email3')
or just add a class to all the elements you want to select ...
source share