JavaScript Email Address Validation

Email addresses can be validated in JavaScript by using a regular expression:

(The following regular expression is actually one line – I have broken it into two so that it will wrap properly in WordPress.)

/^[A-Za-z0-9]+\w*([._-][a-zA-Z0-9]+)*@[A-Za-z0-9]+([._-][a-zA-Z0-9]+)*
\.[a-zA-Z]{2,}$/.test('joey@joey.com') ;

The JavaScript email address validation demonstrated here is somewhat loose to allow for varying email possibilities.

Broken down into sections:

^[A-Za-z0-9]+ – Begin with one or more letter or number characters
\w* – Followed by 0 or more ASCII word characters
([._-][a-zA-Z0-9]+)* – Followed by 0 or more occurrences of an optional period, underscore or hyphen, followed by one or more letter or number characters
@ – Followed by a mandatory alpha character
Domain validation starts here:
[A-Za-z0-9]+ – Followed by one or more letter number characters
([._-][a-zA-Z0-9]+)* – Followed by 0 or more occurrences of an optional period, underscore or hyphen, followed by one or more letter or number characters
\. – Followed by a mandatory period (must have something like .com, .org, .edu, etc.)
[a-zA-Z]{2,}$ – Ended by 2 or more letter characters

Leave a Reply