Link Search Menu Expand Document

How to Validate Email Address via Regex in C#

In this article, you will see how to validate email addresses via regular expressions (REGEX) in C#.

A valid email address has four main parts:

  1. Alphabets and numbers come before the @ symbol and can be separated by dots and other special characters.
  2. An at symbol:@
  3. The domain name e.g. Gmail, Yahoo, etc.
  4. The domain extension e.g .com, .io, .net, etc.

While defining our C# Regex, you need to take care of these four points. The regex that can validate the above four points can be the following:

[a-z A-z 0-9_\-]+

The above expression validates the first part of the email. It makes sure that a string containing small or capital alphabets, or digits from 0-9, or an underscore, a hyphen, or a dot must occur at least once.

[@]

The at symbol “@” must appear after the string is matched in the first step.

[a-z]+

The @ symbol should be followed by small letters ranging from a-z. This is to validate domain names such as Gmail, Yahoo, etc. The plus sign signifies that this pattern should occur at least once at this position.

[\.][a-z]{3,4}$

This regex pattern matches the domain extension e.g. .com, .net. The expression first checks that the string must start with a dot symbol followed by a minimum of 3 and a maximum of 4 small letters from a to z.

Now you know the regex expression needed to validate an email address. Let’s see this regex in action with the help of the C# code.

The script below defines a function named ValidateEmail() which accepts a string that is matched against the regex that validates email addresses. If the email address is valid, the function returns true.

Note: You will need to import the “System.Text.RegularExpressions” module before running the script below.

public static bool ValidateEmail(string email_string)
        {
        var regex = @"[a-z A-z 0-9_\-]+[@]+[a-z]+[\.][a-z]{3,4}$";
            bool result = Regex.IsMatch(email_string, regex, RegexOptions.IgnoreCase);
            return result;
        }

Now let’s validate some dummy email addresses to see if the ValidateEmail() method actually works as expected.

Inside the Main() method in the script below, we define a variable called email. This variable contains a dummy string “@abc.com”. The string is then passed to the ValidateEmail() method to see if it is a valid email address. The result, which will be a boolean value, is printed on the console.

       static void Main(string[] args)
        {
            string email = "@abc.com";
            Boolean is_valid = ValidateEmail(email);
            Console.WriteLine(is_valid);
            Console.ReadLine();
        }

Output:

Validate Email Address

Since the input email didn’t have any string before the @ symbol, you can see that the email could not be validated.

Let’s try another example, set the value of the string variable email to “abc@xyz”, as shown in the script below:

string email = "abc@xyz";

Now if you run the Main() function that you saw in a previous script, you will see False in the output again. This is because our email string does not contain any domain extension such as .com, .net, etc.

Let’s try to add an email extension to our email address as shown below:

string email = "abc@xyz.comic";

The output will again be false since we specified in the regex expression that the domain extension cannot be smaller than 3 characters or greater than 4 characters. However, in the above string, the domain extension “.comic” is 5 characters long. Hence, the email address is not validated.

Let’s try another email with 3 characters in domain extension:

string email = "abc@xyz.com";

In the output, you will see True which shows that the email address has been validated.

Other useful articles:


Back to top

© , Regexsonline.com — All Rights Reserved - Terms of Use - Privacy Policy