Email address validation 2006 February 8 0:32
Posted by diamond in : Random , trackbackThis is an attempt at a definitive javascript regex to check the syntax of an email address. Corrections most welcome. This is based on a reading of this (with additional checking of rfc 2822 for correctness). Here goes:
^[A-Za-z/-9!#-'*+\-/=?_`{-~^]
([A-Za-z.-9!#-'*+\-/=?_`{-~^]*
[A-Za-z/-9!#-'*+\-/=?_`{-~^])?
@([A-Za-z0-9]+\.)+[A-Za-z0-9]+\.?$
(split into multiple lines to prevent horrible formatting)
Note: this is the literal regex. To use it in a javascript string, you’d need to double the \’s for a start.
*Update*
Note: this doesn’t take into account the length limitations in either the local part or domain part. Should i add them?
*Update 2*
From comments from cjb, it now has better (and simpler!) domain parsing.
*Update 3*
From more comments from cjb, some evil (and not so evil) shortening done using character ranges.
Comments»
Thank you _so_ much for all the help.
[…] Stephen’s recently been questioned a bit about his email address validation regex - which has a few problems… Notably, it doesn’t allow for addresses like rob@uk, which I’m assured once existed (although I don’t believe it still does). […]
The regex here is slightly wrong (less wrong than most though
):
- the host portion is wrong, as noirin notes
).
- it’s locale-dependent, e.g. a-z will match non-ASCII chars in many locales, and it’s uses an ASCII-ordering dependent range (very cute, but broken
There’s also some redundancy in the regex which could be avoided.
I’ve had a back and forth with Stephen about this back in March, and I think we sort of agreed on the above points. As he’s not updated this post, I’ve gone and blogged about it.
Thanks