Magento 1.9 Email Validation – How to Validate Comma Separated Email Addresses

emailmagento-1.9validation

i have used System > Configuration > email field

i need to comma-separated validation.

we can validate single email with the help of validate-email but i need to use in comma separated fields.

enter image description here

any solution for system configuration comma-separated email validation ?

Best Answer

You can add custom validation class like below in your js file

Validation.add('validate-comma-separated-emails', 'Please enter a valid email address.', function(emaillist) {
    emaillist = emaillist.trim();
    if(emaillist.charAt(0) == ',' || emaillist.charAt(emaillist.length - 1) == ','){ return false; }
    var emails = emaillist.split(',');
    var invalidEmails = [];
    for (i = 0; i < emails.length; i++) { 
    var v = emails[i].trim();
        if(!Validation.get('validate-email').test(v)) {
            invalidEmails.push(v);
        }
    }
    if(invalidEmails.length){ return false; }
    return true;
});

Now use validate-comma-separated-emails in your text field. Tested with magento admin panel, Output: enter image description here