12

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.

user2256799
  • 229
  • 1
  • 3
  • 10
  • 4
    Why would you want to first validate, then split? It would be much easier to split and then loop through each email. – Jon Snow May 21 '14 at 07:52
  • If you split, the second email will not be valid. It would be something like `email1@gmail.com . email2@gmail.com`. This will not validate when you use the regex I provided. – Anonymoose May 21 '14 at 08:07
  • EMAIL and email@email.com are not comparable strings. – Dani Jan 18 '18 at 20:41

5 Answers5

14

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

Anonymoose
  • 2,389
  • 6
  • 36
  • 69
  • It doesn't validate the list. Also it does not validate "EMAIL,EMAIL , EMAIL " when I do validate the array split result. – user2256799 May 21 '14 at 08:07
  • 2
    It should, what does the list look like that you are trying to validate? – Anonymoose May 21 '14 at 08:08
  • @user2256799, You could remove spaces safely by calling `replace()` before you split the string into an array. `"foo@bar.com, pow@wow.com".replace(" ", "").split(",");` – Ben Keating Nov 16 '16 at 01:40
  • @Flowpoke would use .replace(/\s/g,'') to get rid of all white space in the string and then do the split. works great. – Dani Jan 18 '18 at 20:40
6

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 :)

Sunny_Sid
  • 401
  • 4
  • 13
  • Where have you seen that TLDs are limited to 4 characters? See http://www.iana.org/domains/root/db also, have a look at https://en.wikipedia.org/wiki/Email_address#Examples to see some valid emails not covered by your regex. – Toto Oct 26 '16 at 10:15
  • @Toto - mostly we use .com, .org, .inc, .co.aus, etc. So as per majority I recommend 4, if anyone wanna change according to his domain, otherwise its perfect and never got any problem from my work done. – Sunny_Sid Oct 26 '16 at 11:04
5

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

var emailStr = "felipe@google.com   , felipe2@google.com, emanuel@google.com\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.

Felipe Alarcon
  • 948
  • 11
  • 19
3

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,]+)$

sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • 6
    @user2256799, Well you replace `EMAIL` with the actual email – sshashank124 May 21 '14 at 08:28
  • @sshashank124 please could you help me with this http://stackoverflow.com/questions/23797093/regex-email-validation-that-allows-only-hyphens-in-the-middle-of-the-domain-and – Axel May 22 '14 at 05:27
-1

const regExp = new RegExp(/^(\s?[^\s,]+@[^\s,]+\.[^\s,]+\s?,)*(\s?[^\s,]+@[^\s,]+\.[^\s,]+)$/);
const dom = document.getElementById("regExDom");
const result = regExp.test("test@gmail.com,test2@gmail.com,test3@test.com")
console.log("Regex test => ", result)
dom.innerHTML = result;
<div id="regExDom"></div>
Aditya
  • 395
  • 2
  • 6