jump to navigation

Email address validation 2006 February 8 0:32

Posted by diamond in : Random , 3 comments

This 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.