Regex: Email Format Validation

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Regex: Email Format Validation

Post by Chris Corbyn »

chandan wrote:uses this to validate ',' (comma) seperated email addresses. this regular expression not yet performance tuned, but should be good for validating emails.

Expression: ^(\w+(.|_)\w+@\w+\.\w+)(,(\w+(.|_)\w+@\w+\.\w+)|\S)+$
Dont forget to add escape sequence to suit to your env.

-Chandan Benjaram
That regex doesn't verify that the email addresses have a valid syntax in accordance with RFC 2822 like previously posted examples. It simply checks that they look like words with an @ sign in the middle. The (.|_) is pointless by the way. _ is covered by \w anyway, and . simply allows something like a dodgy octet.

The RFC2822 compliance thing I'm not too fussed about since I only check for ascii chars with an @ sign in the middle -- if the user doesn't get my email then they should specify a valid address. But the use of "." makes your validation vulnerable; depending upon it's usage.
Post Reply