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