How to: Verify That Strings are in Valid E-Mail Format

The following example verifies that a string is in valid e-email format. Note that the regular expression is not able to validate email addresses that contain Unicode characters outside the US-ASCII character range.

The example defines an IsValidEmail method, which returns true if the string contains a valid e-mail address and false if it does not, but takes no other action. To verify that the e-mail address is valid, the method calls the Regex.IsMatch(String, String) method to verify that the address conforms to a regular expression pattern. You can use IsValidEmail to filter out e-mail addresses that contain invalid characters before your application stores the addresses in a database or displays them in an ASP.NET page.

Note that the IsValidEmail method does not perform authentication to validate the e-mail address. It merely determines whether its format is valid for an e-mail address.

using System;
using System.Text.RegularExpressions;

public class RegexUtilities
   public static bool IsValidEmail(string strIn)
      // Return true if strIn is in valid e-mail format.
      return Regex.IsMatch(strIn, 
              @"^(?("")(""[^""]+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" + 

In this example, the regular expression pattern ^(?("")(""[^""]+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$ can be interpreted as shown in the following table.




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 other than a quotation mark, followed by an ending quotation mark. The string should conclude with an at sign (@).


If the first character is not a quotation mark, match any alphabetic character from a to z 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 e-mail address.


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


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.


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


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


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.


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 an 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 should be followed by two to six alphabetic (a-z, A-Z) characters. This portion of the regular expression is designed to capture the domain name.

The IsValidEmail method can be included in a library of regular expression utility methods, or it could be included as a private static or instance method in the application class. If it is used as a static method in a regular expression library, it can be called by using code such as the following:

public class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      string[] emailAddresses = { "", "", 
                                  "", "", 
                                  "j@proseware.com9", "", 
                                  "j_9@[]", "", 
                                  "js*", "", 
                                  "js@proseware.com9", "" };
      foreach (string emailAddress in emailAddresses)
         if (RegexUtilities.IsValidEmail(emailAddress))
            outputBlock.Text += String.Format("Valid: {0}\n", emailAddress);
            outputBlock.Text += String.Format("Invalid: {0}\n", emailAddress);
// The example displays the following output:
//       Valid:
//       Valid:
//       Valid:
//       Invalid:
//       Invalid: j@proseware.com9
//       Valid:
//       Valid: j_9@[]
//       Invalid:
//       Invalid: js*
//       Invalid:
//       Invalid: js@proseware.com9
//       Valid:

Community Additions