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:
- Alphabets and numbers come before the @ symbol and can be separated by dots and other special characters.
- An at symbol:@
- The domain name e.g. Gmail, Yahoo, etc.
- 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:
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:
- How to Use RegEx for Data Extraction
- How to Find Total Tax Using a Regular Expression in C#
- How to Find a Number Using Regular Expressions in C#
- How to Find Invoice Numbers Using Regular Expressions in C#
- Find SSN Using a Regular Expression in C#
- Find Total Amount Using a Regular Expression in C#
- How to Find Website Links using Regex
- Module 1: Regular Expressions for Beginners
- Module 1: Regex Usage and Tool Demo
- Module 2: Regex Engine Basics (Part 1)
- Module 2: Regex Engine Basics (Part 2)
- Module 2: Regex Syntax in Detail (Part 1)
- Module 2: Regex Syntax in Detail (Part 2)
- Module 2: Quantifiers in Reg Ex for Beginners
- Module 2: Short Codes in Reg Ex for Beginners
- Module 2: Anchors and Boundaries in Detail
- Module 2: Grouping and Subpattern in Detail
- Module 3: Realtime Use Case of Regular Expressions - Part 1
- Module 3: Realtime Use Case of Regular Expressions - Part 2
- Module 3: Realtime Use Case of Regular Expressions - Part 3
- Module 3: Realtime Use Case of Regular Expressions - Part 4
- How to Find Quantity Field Using Regular Expression in C#
- How to Find Phone Numbers without a Specific Format
- How to Find Date Using Regular Expression in C#
- How to Find Time Using Regular Expression in C#
- How to Find a Sentence Using Regular Expressions in C#
- Find a Word Using Regular Expression in C#
- Find a Due Date using Regular Expressions in C#
- How to Find the End of a String Using Regular Expression in C
- How to Find the Start of a String Using Regular Expression in C
- How to Find a Comma using Regular Expression in C Sharp
- How to Find a Dot using Regular Expression in C
- How to Find a Semicolon using Regular Expression in C Sharp
- How to Find a Double Space using Regular Expression in C
- How to Split Text Using Regex
- How to Find HTML Tags Using Regex
- How to Validate Email Address via Regex in C#