Categories
Development

Email validation Regexes

Now we want to review some email validation Regexes. We’ve chosen Regexes based on readability, complexity and RFC standarts relevance. For online Regex testing tools refer here.

General patterns

  • Simple pattern –  it works for major currently-used emails. It doesn’t validate IP-Addresses-based emails though :
^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$
  • Another pattern –  but when using it, be sure to lower the uppercase letters in the email prior to validation:
^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$
  • This pattern is more inclusive, yet again not allowing TLD (top level domain) to host mail addresses; like ‘nathan@dk’.
/\b[!#\$%&'\*\+\-/=\?\^_`{\|}~a-zA-Z0-9]
[!#\$%&'\*\+\-/=\?\^_`{\|}~a-zA-Z0-9\.]*
[!#\$%&'\*\+\-/=\?\^_`{\|}~a-zA-Z0-9]@[a-zA-Z0-9\-]
[a-zA-Z0-9\-\.]+[a-zA-Z0-9\-]\b/g
  • Some email validation Regexes are user contributed in RegexLib online library, having appropriate descriptions and use-boundary defined. There are many Regexes having either simple expression functions or more complex functions there.

RFC standards compliant

  • This Regex pattern for email validation is 100% RFC-2822 (RFC – Request For Comments) compliant:

/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/g

  • The monster Regex for validating emails is here. It was generated by the Perl module by linking together a simpler set of regular expressions that relate directly to the grammar defined in the RFC-822.
  • The latest RFC on internet message format is the RFC-5322 (October 2008). This specification is a revision of Request For Comments RFC-2822, which itself superseded RFC-822.

JavaScript- and Perl-compatible

/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/

ASP.NET

This one is used in ASP.NET by the RegularExpressionValidator:
\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

HTML5

The Regex compliant to the HTML5 spec. Read more here – w3.org.
[a-zA-Z0-9.!#$%&'*+-/=?\^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*

German email validator

Matches email addresses, which have ä ö ü Ä Ö Ü in name or domain:

/([-a-z0-9öäüÖÄÜ\.]+)@((?:[-a-z0-9öäüÖÄÜ\.]+\.)+)([a-zA-Z]{2,4})/gi

Note on browser makers’ validation issue

Email validators are being used for many applications to match the strict yet inclusive RFC specifications. However, the latest trend is that many browser makers often ignore RFC’s standarts and redefine what is a valid email themselves. When creating INPUT TYPE=”email”, some makers allow only a simply-validated, reduced subset of email address formats, thus frustrating the users in normal web interaction when entering emails.

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.