jQuery Validate unique email addresses

jQuery Validate offers a lot of great features out of the box, but one feature it’s lacking is validating if something is not found is a list. Fortunately, it’s super easy to add your own validators. In my case I want to find if something doesn’t exist in a list.

jQuery.validator.addMethod('uniqueEmail', function(value) {
    var currentEmails = 'an array from somewhere with emails lower cased';
    //NOTE: this check is case sensitive, so make sure you lower case both values being compared
    return $.inArray(value.toLowerCase(), currentEmails) === -1;
}, 'Email has already been added.')

$('#ccEmail').validate({
    errorElement: 'div',
    rules: {
        CCEmail_Address: {
            email: true,
            uniqueEmail: true //use the custom validator
        }
    }
});
Matt Busche's Picture

About Matt Busche

Software Engineer and Wheel of Fortune Expert If this article helped you, please consider buying me a book.

Des Moines, IA https://www.mrbusche.com