Member Sign In
Not a member?

A Wired.com user account lets you create, edit and comment on Webmonkey articles. You will also be able to contribute to the Wired How-To Wiki and comment on news stories at Wired.com.


It's fast and free.

Sign in with OpenID
Sign In
Webmonkey is a property of Wired Digital.
processing...
Join Webmonkey

Please send me occasional e-mail updates about new features and special offers from Wired/Webmonkey.
Yes No

Please send occasional e-mail offers from Wired/Webmonkey affiliated web sites and publications, and carefully selected companies.
Yes No

I understand and agree that registration on or use of this site constitutes agreement to Webmonkey's User Agreement and Privacy Policy.
Webmonkey is a property of Wired Digital.
processing...

Retrieve Sign In

Please enter your e-mail address or username below. Your username and password will be sent to the e-mail address you provided us.

or
Webmonkey is a property of Wired Digital.
processing...

Welcome to Webmonkey

A private profile page has been created for you.
As a member of Webmonkey, you can now:
  • edit articles
  • add to the code library
  • design and write a tutorial
  • comment on any Webmonkey article
Close
Webmonkey is a property of Wired Digital.

Sign In Information Sent

An e-mail has been sent to the e-mail address registered in this account.
If you cannot find it in your in-box, please check your bulk or junk folders.
Sign In
Webmonkey is a property of Wired Digital.

Four Regular Expressions to Check Email Addresses

How do you find out if a user has entered a valid email address? Do you check for an at-sign, or is it more complicated? For many developers the answer is a regular expression, a little bit of code that can describe text patterns using wildcards and other special characters. If you’re new to the topic, we have a great regular expression tutorial.

Here are four regular expressions (often called regexes) that all validate the format of an email address. They have increasing degrees of complexity. The more complicated, the more accurate each is at matching only email addresses.

1. Dirt-simple approach

Here’s a regex that only requires a very basic xxxx@yyyy.zzz:

.+\@.+\..+

Upside: Dirt simple.

Downside: Even invalid email addresses like xxxx@yyyy.zzz, or even a@b.c, make it through.

2. Slightly more strict (but still simple) approach

Regular-Expressions.Info provides a basic email validation regex that tries to be a little smarter:

[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}

Upside: Only allows email address-friendly characters, restricts domain extension to only two to four characters.

Downside: It still allows many invalid email addresses, and misses some longer domain extensions (.museum, for example).

3. Specify all the domain extensions approach

Reddit user teye points to his regex, which only allows domain extensions that actually exist:

([a-z0-9][-a-z0-9_\+\.]*[a-z0-9])@([a-z0-9][-a-z0-9\.]*[a-z0-9]\.(arpa|root|aero|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|([0-9]{1,3}\.{3}[0-9]{1,3}))

Upside: It doesn’t allow xxxx@yyyy.zzz!

Downside: Upkeep could be tough with this one. You’d have to update any time new domain extensions are announced. In fact, you already would need to add the .me extension.

4. Way complicated approach

A Perl module has a long regular expression based on the standard description of an email address. It’s so long (nearly 6,500 characters!) that I won’t include it here.

Upside: It’s complete.

Downside: It’s way complicated.

Meet in the middle approach

You’ll have to decide, if you haven’t already, which regular expression to use. Likely, you’ll choose somewhere in the middle of the examples we’ve given. Regular-Expressions.Info has a good run-down of the trade-offs of different approaches.

Have you already decided how to check email addresses? How do you do it?

[via Reddit]

See also:

Post Comment Comments Permalink Print
Reddit Digg

 
Subscribe now

Special Offer For Webmonkey Users

WIRED magazine:
The first word on how technology is changing our world.

Subscribe for just $10 a year