-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. [email protected], [email protected], [email protected]

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

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:

[email protected]; [email protected], [email protected]; [email protected] ; 
dd.dd.ddwe.wscef_sed@_e23&&*^.dvcw
kayleeFrye_onDeck
  • 6,648
  • 5
  • 69
  • 80