-1

I have this RegEx which I use for CC and BCC email fields

reg = /(^\s*$|^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.(?:[a-zA-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)$)/;

This allows for the email field to be empty, or have a valid email address, otherwise it will error.

I would like to extend the RegEx to allow mutiple emails also e.g. a@a.com, b@b.com, c@c.com

I have tried adding [,;] to allow comma or semicolon seperated values, but i can't seem to get it to work.

Any one know if i'm on the right lines with [,;] and where I should be placing it?

Update: I've updated the RegEx to, so it doesn't look for gTLDs:

reg = /(^\s*$|^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[A-Za-z]{2,4}[,;]?)+$/;

thanks

  • 2
    Never test for TLD names like that, your list is missing dozens of perfectly valid ones – Alex K. Nov 25 '14 at 15:19
  • I'm still learning RegEx, any advice on how to check for all names? – jamesmartin82 Nov 25 '14 at 15:21
  • What language/environment are you in? – Alex K. Nov 25 '14 at 15:26
  • You can't properly validate e-mails with regex unless you constantly update your regex to match all the new gTLDs that are coming. – ohaal Nov 25 '14 at 15:29
  • It's used within a function in javascript. The project is in asp. – jamesmartin82 Nov 25 '14 at 15:35
  • @AlexK. Yup. I have a .london email address. There are a very large and now quite commonly changing set of extensions. - http://data.iana.org/TLD/tlds-alpha-by-domain.txt – Sam Greenhalgh Nov 25 '14 at 15:36
  • Loose validation in JS E.g. `\S+@\S+` then stricter validation on the server; http://stackoverflow.com/questions/1710505/asp-net-email-validator-regex – Alex K. Nov 25 '14 at 15:42
  • amended the RegEx to `reg = /(^\s*$|^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[A-Za-z]{2,4}[,;]?)+$/;` this won't look for gTLDs, which is not massively important in this project. But i still can't get it to allow allow mutiple emails also e.g. a@a.com, b@b.com, c@c.com – jamesmartin82 Nov 25 '14 at 16:07

1 Answers1

1

If Alex K.'s comment about ASP.NET validation doesn't help, then I have a band-aid for you. I wouldn't consider this a proper answer, as there really isn't a way to get exactly the functionality that you're looking for without giving us all pre and post email special characters that can occur. You could use something like this that uses non-capture groups to help find matches. It's not 100% accurate, but it should work for most cases. One problem with it is that you're apt to capture garbage/non-desired results if it runs into stray @ symbols.

regex tested by RegexBuddy 4.2.0:

(?m)(?:^|\s|\n|\t|\r|,|;|
)[^\n]*?@[^\n]*?\.[^\n]*?(?:$|;|\s|,)

Test strings used:

9som$emaIL@cm3Gks.qa1vv; 9som$emaIL@cm3Gks.qa1vv, 9som$emaIL@cm3Gks.qa1vv; 9som$emaIL@cms.com ; 
dd.dd.ddwe.wscef_sed@_e23&&*^.dvcw
kayleeFrye_onDeck
  • 6,648
  • 5
  • 69
  • 80