Follow me
Latest replies
Categories
« System.TypeLoadException | Main | How to configure Windows Azure Samples to use SQL Server 2005 »
Sunday
16Nov

RegEx and email validation in .NET

Here's another one:

static readonly Regex _emailRegex = new Regex(
  @"^[a-z0-9](([_\.\-\'\+]?[a-z0-9]+)*)" +
  @"@(([a-z0-9]{1,64})(([\.\-][a-z0-9]{1,64})*)\.([a-z]{2,})" +
  @"|(\d{1,3}(\.\d{1,3}){3}))$",
  RegexOptions.Compiled | RegexOptions.Singleline 
    | RegexOptions.IgnoreCase);

This RegEx accepts these proper emails:

  • l3tt3rsAndNumb3rs@domain.com
  • has-dash@domain.com
  • hasApostrophe.o'leary@domain.org
  • uncommonTLD@domain.museum
  • uncommonTLD@domain.travel
  • uncommonTLD@domain.mobi
  • countryCodeTLD@domain.uk
  • lettersInDomain@911.com
  • underscore_inLocal@domain.net
  • IPInsteadOfDomain@127.0.0.1
  • subdomain@sub.domain.com
  • local@dash-inDomain.com
  • dot.inLocal@foo.com
  • a@singleLetterLocal.org
  • singleLetterDomain@x.org
  • disposable+email@gmail.com

And these invalid emails are rejected:

  • two@@signs.com
  • missingDomain@.com
  • @missingLocal.org
  • missingatSign.net
  • missingDot@com
  • colonButNoPort@127.0.0.1:
  • [Empty]
  • someone-else@127.0.0.1.26
  • .localStartsWithDot@domain.com
  • localEndsWithDot.@domain.com
  • two..consecutiveDots@domain.com
  • domainStartsWithDash@-domain.com
  • domainEndsWithDash@domain-.com
  • numbersInTLD@domain.c0m
  • missingTLD@domain.
  • ! ""#$%(),/;[]`|@invalidCharsInLocal.org
  • invalidCharsInDomain@! ""#$%(),/;<>_[]`|.org
  • local@SecondLevelDomainNamesAreInvalidIfTheyAreLongerThan64Charactersss.org

If you are using Lokad Shared Libraries, then you can reference this email validation rule directly in your enforce statements like:

Enforce.Argument(email, "email", StringRules.ValidEmail);

or build more complex and reusable validation rules:

Enforce.Argument(account, "account", ValidAccount);

// ...
public static void ValidAccount(Account account, IScope scope)
{
  scope.Validate(account.Username, "Username", 
    StringRules.Limit(6, 256), 
    StringRules.ValidEmail);

  scope.Validate(account.Password, "Password", 
    StringRules.Limit(1, 256));
}

References (1)

References allow you to track sources for this article, as well as articles that were written in response to this article.

Reader Comments (2)

This doesn't correctly validate james+foo@example.org. This is A) valid and B) important to me. I can sign up for a site using MyGmailUsername+customId@gmail.com and all those custom Id's go to my inbox and I can filter based on them.

Validating an email according to the RFC is nontrivial and we probably shouldn't be rolling our own implementations.

Of course if we just don't care about the RFC and just wanna get it mostly right... well that can be a valid choice too sometimes.

See:
http://lifehacker.com/software/gmail/instant-disposable-gmail-addresses-144397.php
http://haacked.com/archive/2007/08/21/i-knew-how-to-validate-an-email-address-until-i.aspx

November 16, 2008 | Unregistered CommenterJames Thigpen

James,

thank you for pointing out this Disposable email trick!

I've also updated the pattern and test cases.

As for the completely RFC-compliant implementations... I'm aware of these, but needed something a little bit less complex and more fast for the email validation in .NET. That's for the situations when emails are used as Logins and must be validated on every web service request.

November 17, 2008 | Registered CommenterRinat Abdullin
Comments for this entry have been disabled. Additional comments may not be added to this entry at this time.