Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How to: Verify that Strings Are in Valid Email Format

The following example uses a regular expression to verify that a string is in valid email format.

The example defines an IsValidEmail method, which returns true if the string contains a valid email address and false if it does not, but takes no other action.

To verify that the email address is valid, the IsValidEmail method calls the Regex.Replace(String, String, MatchEvaluator) method with the (@)(.+)$ regular expression pattern to separate the domain name from the email address. The third parameter is a MatchEvaluator delegate that represents the method that processes and replaces the matched text. The regular expression pattern is interpreted as follows.

Pattern

Description

(@)

Match the @ character. This is the first capturing group.

(.+)

Match one or more occurrences of any character. This is the second capturing group.

$

End the match at the end of the string.

The domain name along with the @ character is passed to the DomainMapper method, which uses the IdnMapping class to translate Unicode characters that are outside the US-ASCII character range to Punycode. The method also sets the invalid flag to True if the IdnMapping.GetAscii method detects any invalid characters in the domain name. The method returns the Punycode domain name preceded by the @ symbol to the IsValidEmail method.

The IsValidEmail method then calls the Regex.IsMatch(String, String) method to verify that the address conforms to a regular expression pattern.

Note that the IsValidEmail method does not perform authentication to validate the email address. It merely determines whether its format is valid for an email address. In addition, the IsValidEmail method does not verify that the top-level domain name is a valid domain name listed at the IANA Root Zone Database, which would require a look-up operation. Instead, the regular expression merely verifies that the top-level domain name consists of between two and twenty-four ASCII characters, with alphanumeric first and last characters and the remaining characters being either alphanumeric or a hyphen (-).

using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class RegexUtilities
{
   bool invalid = false;

   public bool IsValidEmail(string strIn)
   {
       invalid = false;
       if (String.IsNullOrEmpty(strIn))
          return false;

       // Use IdnMapping class to convert Unicode domain names. 
       try {
          strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper,
                                RegexOptions.None, TimeSpan.FromMilliseconds(200));
       }
       catch (RegexMatchTimeoutException) {
         return false;
       }

        if (invalid)
           return false;

       // Return true if strIn is in valid e-mail format. 
       try {
          return Regex.IsMatch(strIn,
                @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
                @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
                RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
       }
       catch (RegexMatchTimeoutException) {
          return false;
       }
   }

   private string DomainMapper(Match match)
   {
      // IdnMapping class with default property values.
      IdnMapping idn = new IdnMapping();

      string domainName = match.Groups[2].Value;
      try {
         domainName = idn.GetAscii(domainName);
      }
      catch (ArgumentException) {
         invalid = true;
      }
      return match.Groups[1].Value + domainName;
   }
}

In this example, the regular expression pattern ^(?(")(".+?(?<!\\)"@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$ is interpreted as shown in the following table. Note that the regular expression is compiled using the RegexOptions.IgnoreCase flag.

Pattern

Description

^

Begin the match at the start of the string.

(?(")

Determine whether the first character is a quotation mark. (?(") is the beginning of an alternation construct.

(?("")("".+?(?<!\\)""@)

If the first character is a quotation mark, match a beginning quotation mark followed by at least one occurrence of any character, followed by an ending quotation mark. The ending quotation mark must not be preceded by a backslash character (\). (?<! is the beginning of a zero-width negative lookbehind assertion. The string should conclude with an at sign (@).

|(([0-9a-z]

If the first character is not a quotation mark, match any alphabetic character from a to z or A to Z (the comparison is case insensitive), or any numeric character from 0 to 9.

(\.(?!\.))

If the next character is a period, match it. If it is not a period, look ahead to the next character and continue the match. (?!\.) is a zero-width negative lookahead assertion that prevents two consecutive periods from appearing in the local part of an email address.

|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w]

If the next character is not a period, match any word character or one of the following characters: -!#$%'*+=?^`{}|~.

((\.(?!\.))|[-!#\$%'\*\+/=\?\^`\{\}\|~\w])*

Match the alternation pattern (a period followed by a non-period, or one of a number of characters) zero or more times.

@

Match the @ character.

(?<=[0-9a-z])

Continue the match if the character that precedes the @ character is A through Z, a through z, or 0 through 9. The (?<=[0-9a-z]) construct defines a zero-width positive lookbehind assertion.

(?(\[)

Check whether the character that follows @ is an opening bracket.

(\[(\d{1,3}\.){3}\d{1,3}\])

If it is an opening bracket, match the opening bracket followed by an IP address (four sets of one to three digits, with each set separated by a period) and a closing bracket.

|(([0-9a-z][-\w]*[0-9a-z]*\.)+

If the character that follows @ is not an opening bracket, match one alphanumeric character with a value of A-Z, a-z, or 0-9, followed by zero or more occurrences of a word character or a hyphen, followed by zero or one alphanumeric character with a value of A-Z, a-z, or 0-9, followed by a period. This pattern can be repeated one or more times, and must be followed by the top-level domain name.

[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))

The top-level domain name must begin and end with an alphanumeric character (a-z, A-Z, and 0-9). It can also include from zero to 22 ASCII characters that are either alphanumeric or hyphens.

$

End the match at the end of the string.

Note Note

Instead of using a regular expression to validate an email address, you can use the System.Net.Mail.MailAddress class. To determine whether an email address is valid, pass the email address to the MailAddress.MailAddress(String) class constructor.

The IsValidEmail and DomainMapper methods can be included in a library of regular expression utility methods, or they can be included as private static or instance methods in the application class.

To include them in a regular expression library, either copy and paste the code into a Visual Studio Class Library project, or copy and paste it into a text file and compile it from the command line with a command like the following (assuming that the name of the source code file is RegexUtilities.cs or RegexUtilities.vb:

csc /t:library RegexUtilities.cs

You can also use the Regex.CompileToAssembly method to include this regular expression in a regular expression library.

If they are used in a regular expression library, you can call them by using code such as the following:

public class Application
{
   public static void Main()
   {
      RegexUtilities util = new RegexUtilities();
      string[] emailAddresses = { "david.jones@proseware.com", "d.j@server1.proseware.com",
                                  "jones@ms1.proseware.com", "j.@server1.proseware.com",
                                  "j@proseware.com9", "js#internal@proseware.com",
                                  "j_9@[129.126.118.1]", "j..s@proseware.com",
                                  "js*@proseware.com", "js@proseware..com",
                                  "js@proseware.com9", "j.s@server1.proseware.com",
                                   "\"j\\\"s\\\"\"@proseware.com", "js@contoso.中国" };

      foreach (var emailAddress in emailAddresses) {
         if (util.IsValidEmail(emailAddress))
            Console.WriteLine("Valid: {0}", emailAddress);
         else
            Console.WriteLine("Invalid: {0}", emailAddress);
      }                                            
   }
}
// The example displays the following output: 
//       Valid: david.jones@proseware.com 
//       Valid: d.j@server1.proseware.com 
//       Valid: jones@ms1.proseware.com 
//       Invalid: j.@server1.proseware.com 
//       Valid: j@proseware.com9 
//       Valid: js#internal@proseware.com 
//       Valid: j_9@[129.126.118.1] 
//       Invalid: j..s@proseware.com 
//       Invalid: js*@proseware.com 
//       Invalid: js@proseware..com 
//       Valid: js@proseware.com9 
//       Valid: j.s@server1.proseware.com 
//       Valid: "j\"s\""@proseware.com
//       Valid: js@contoso.中国

Assuming you've created a class library named RegexUtilities.dll that includes your email validation regular expression, you can compile this example in either of the following ways:

  • In Visual Studio, by creating a Console Application and adding a reference to RegexUtilities.dll to your project.

  • From the command line, by copying and pasting the source code into a text file and compiling it with a command like the following (assuming that the name of the source code file is Example.cs or Example.vb:

    csc Example.cs /r:RegexUtilities.dll
    
Show:
© 2014 Microsoft