user2256799
user2256799

Reputation: 229

Validate a comma separated email list

I'm trying to come up with a regular expression to validate a comma separated email list.

I would like to validate the complete list in the first place, then split(";"), then trim each array value (each email) from the split.

I would like to validate the following expressions:

EMAIL,EMAIL  --> Ok
EMAIL, EMAIL  --> Ok
EMAIL , EMAIL  --> Ok
EMAIL , , EMAIL  --> Wrong
EMAIL , notAnEmail , EMAIL  --> Wrong

I know there's many complex expressions for validating emails but I don't need anything fancy, this just works for me: /\S+@\S+\.\S+/;

I would like plain and simple JS, not jQuery. Thanks.

Edit: I've already considered fist validate then split, but with the expressions I've tried so far, this will be validated as two correct emails:

EMAIL, EMAIL  .  EMAIL

I would like to validate the list itself as much as every email.

Upvotes: 12

Views: 33160

Answers (5)

Aditya
Aditya

Reputation: 415

const regExp = new RegExp(/^(\s?[^\s,]+@[^\s,]+\.[^\s,]+\s?,)*(\s?[^\s,]+@[^\s,]+\.[^\s,]+)$/);
const dom = document.getElementById("regExDom");
const result = regExp.test("[email protected],[email protected],[email protected]")
console.log("Regex test => ", result)
dom.innerHTML = result;
<div id="regExDom"></div>

Upvotes: -1

Anonymoose
Anonymoose

Reputation: 2471

An easier way would be to remove spaces and split the string first:

var emails = emailList.replace(/\s/g,'').split(",");

This will create an array. You can then iterate over the array and check if the element is not empty and a valid emailadres.

var valid = true;
var regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

for (var i = 0; i < emails.length; i++) {
     if( emails[i] == "" || ! regex.test(emails[i])){
         valid = false;
     }
}

note: I got the the regex from here

Upvotes: 14

Felipe Alarcon
Felipe Alarcon

Reputation: 956

Also note that you'll want to get rid of extra spaces so an extra step needs to be added as follows:

var emailStr = "[email protected]   , [email protected], [email protected]\n";


function validateEmailList(raw){
    var emails = raw.split(',')


    var valid = true;
    var regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

    for (var i = 0; i < emails.length; i++) {
        if( emails[i] === "" || !regex.test(emails[i].replace(/\s/g, ""))){
            valid = false;
        }
    }
    return valid;
}


console.log(validateEmailList(emailStr))

By adding .replace(/\s/g, "") you make sure all spaces including new lines and tabs are removed. the outcome of the sample is true as we are getting rid of all spaces.

Upvotes: 5

Sunny_Sid
Sunny_Sid

Reputation: 411

I am using the Regex from many years and code is same. I believe this works for every one:

^(\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{2,4}\s*?,?\s*?)+$

Be happy :)

Upvotes: 6

sshashank124
sshashank124

Reputation: 32189

I agree with @crisbeto's comment but if you are sure that this is how you want to do it, you can do it by matching the following regex:

^(\s?[^\s,]+@[^\s,]+\.[^\s,]+\s?,)*(\s?[^\s,]+@[^\s,]+\.[^\s,]+)$

Upvotes: 3

Related Questions