Javascript – validate multi-email

Validate CSV or multi-email using JavaScript:

function check_multi_email($mulEmailField) {
    // Check if each recipient address is valid
    //var emailRegexp = /^([^ <]+@([^ ]+)\.([a-zA-Z0-9\.\-\_]+)|\".+\"\s*)$/;
    var emailRegexp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var slash = /[\\\/]/g;
    var em;
    var em_error = "Invalid e-mail address provided.\nPlease note e-mail addresses should be in this format:\nmail@domain.com\n\n";
    var em_count = 0;

    var emailVal = $( $mulEmailField ).val();
    if($.trim(emailVal) === "") {
    	return true;
    }
    var emailArr = emailVal.split(/[;,]/);

    for (em = 0; em < emailArr.length; em++) {
        emailArr[em] = emailArr[em].replace(/^\s+/, '').replace(/\s+$/, '');
        if (! emailArr[em].match(emailRegexp) || emailArr[em].match(slash)) {
            em_error += emailArr[em] + "\n";
            em_count++;
        }
    }
    if (em_count > 0) {
        alert(em_error);
        $mulEmailField.focus();
        return false;
    }    
    return true;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s