A Better Email Address Regular  Expression

A simple pattern I use to validate emails.

Validating emails with regular expressions can get pretty hairy and complex. If you’re looking for a highly complete and complex approach, then this may not be for you. I’d suggest taking a look at the General Email Regex (RFC 5322 Official Standard) provided at emailregex.com. If you want a simple approach that’s loose enough for use with web forms, yet strict enough to prevent false positives, then this is the approach I’d recommend.

Regular Expression

I’ve found that this regular expression provides just the right balance of loose and strict pattern recognition to safely be used as a method for validation of an email address.

[^@]{2,64}@[^.]{2,253}\.[0-9a-z-.]{2,63}

It’s simpler than most regex email patterns you’ll find on the web. It relies on negated character classes to match anything that is not a specific character. Using this I’m able to loosely verify emails with this approach: {anything}@{anything}.

I didn’t come to this conclusion lightly, it’s been adjusted over 10+ years based on issues and edge cases reported to me by clients. It may or may not be the best approach for your use case, use it at your own discretion.

How It Works

Here’s a breakdown of the criteria that this will test for:

  • Between 2 and 64 characters that aren’t @
  • The character @
  • Between 2 and 253 characters that aren’t .
  • The character .
  • Between 2 and 63 lowercase letters, numbers, dashes or period characters

Demo Tool

Try it out yourself with this demo at regex101.com.

Test it out and see if it’s the right fit for your goals, which it may or may not be.

Other Approaches

I’ve found that this regular expression provides just the right balance of loose and strict pattern recognition to safely be used as a method for validation of an email address. There are many other options out there, if you think this approach won’t work for you then I highly recommend the following sources for more information:

Meet the Author

Kevin Leary, WordPress Consultant

I'm a custom WordPress web developer and analytics consultant in Boston, MA with 17 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.