---
title: "How to validate email in C#?"  
description: "How to validate email in C#?"  
author: "Ravi Vishwakarma"  
published: 2024-06-07  
updated: 2024-06-07  
canonical: https://www.mindstick.com/interview/33892/how-to-validate-email-in-c-sharp  
category: "c#"  
tags: ["c#", ".net"]  
reading_time: 4 minutes  

---

# How to validate email in C#?

Validating an email address in C# can be achieved using regular expressions.

Here's a simple example of how you can do it:

```cs
using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {

        public static void Main(string[] args)
        {
            // Array of email addresses to be validated
            string[] emails = {
                "test@e&&&&xample.com&******",
                "user@domain.com",
                "john.doe@example.com",
                "@3jane_doe1234@gmail.com",
                "testuser123@hotmail.com",
                "info@website.net",
                "support@company.org",
                "admin@system.co.uk",
                "contact_us@example.org",
                "sales@business.com",
                "valid.email+alias@gmail.com",
                "user.name@domain.co",
                "firstname.lastname@sub.domain.com",
                "user@subdomain.example.com",
                "customer.service@domain.in",
                "employee@company.xyz",
                "first.last@company.travel",
                "someone@domain.museum",
                "info@domain.edu",
                "example@domain.io",
                "name@do#main.travel",
                "contact@domain.gov",
                "firstname+lastname@example.com",
                "test.email@domain.co.jp",
                "username@domain.co.uk",
                "name@domain.co.kr",
                "user@domain.co.in",
                "email@sub.domain.biz",
                "contact@domain.jobs",
                "firstname-lastname@example.com",
                "valid_123@domain.com",
                "user.name+tag@domain.co.uk",
                "username@sub.domain.travel",
                "special_chars@example.io",
                "customer-service@domain.tech",
                "info@nonprofit.org",
                "firstname_lastname@domain.co",
                "email@domain.guru",
                "admin@sub.example.net",
                "test@domain.solutions",
                "firstname.lastname@domain.us",
                "user@domain.company",
                "na%me@domain.expert",
                "support@domain.firm",
                "contact@domain.store",
                "email@domain.agency",
                "person@domain.education",
                "user@domain.center",
                "user@domain.pro",
                "name@domain.club",
                "person@domain.media",
                "user@domain.tools",
                "firstname_lastname@domain.estate",
                "example@domain.tech",
                "test@domain.events",
                "person@domain.services",
                "contact@domain.site",
                "info@domain.community",
                "sales@domain.shop",
                "service@domain.holdings"
            };

            // Create an instance of the Program class
            Program pr = new Program();

            // Iterate through each email address in the array
            foreach (var email in emails)
            {
                // Check if the email is valid using the IsValidEmail method
                if (pr.IsValidEmail(email))
                {
                    //Reset th console txt color
                    Console.ResetColor();
                    // Print if the email is valid
                    Console.WriteLine(string.Format("Valid: {0}", email));
                }
                else
                {
                    //Set console text output color for invalid emails
                    Console.BackgroundColor = ConsoleColor.Red;
                    // Print if the email is invalid
                    Console.WriteLine(string.Format("Invalid: {0}", email));
                }
            }
            // Wait for user input to close the console window
            Console.ReadLine();
        }

        /// <summary>
        /// Method to check whether an email is valid or not
        /// </summary>
        /// <param name="email">A string containing the email address to validate</param>
        /// <returns>True if the email is in a valid format, otherwise false</returns>
        bool IsValidEmail(string email)
        {
            // Regular expression pattern for validating email addresses
            string pattern = @"^[\w.+-]+@[\w-]+\.[\w.-]+$";
            // Create a Regex object
            Regex regex = new Regex(pattern, RegexOptions.Multiline);
            // Use the IsMatch method to validate the email address
            return regex.IsMatch(email);
        }
    }
}
```

## Output :

![How to validate email in C#?](https://www.mindstick.com/interviewquestion/4980283b-16a0-4ce2-ba9e-b02891ada53d/images/f6809f67-cfc3-479d-96a1-6d7b895e247b.png)

## Answers

### Answer by Ravi Vishwakarma

Validating an email address in C# can be achieved using regular expressions.

Here's a simple example of how you can do it:

```cs
using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {

        public static void Main(string[] args)
        {
            // Array of email addresses to be validated
            string[] emails = {
                "test@e&&&&xample.com&******",
                "user@domain.com",
                "john.doe@example.com",
                "@3jane_doe1234@gmail.com",
                "testuser123@hotmail.com",
                "info@website.net",
                "support@company.org",
                "admin@system.co.uk",
                "contact_us@example.org",
                "sales@business.com",
                "valid.email+alias@gmail.com",
                "user.name@domain.co",
                "firstname.lastname@sub.domain.com",
                "user@subdomain.example.com",
                "customer.service@domain.in",
                "employee@company.xyz",
                "first.last@company.travel",
                "someone@domain.museum",
                "info@domain.edu",
                "example@domain.io",
                "name@do#main.travel",
                "contact@domain.gov",
                "firstname+lastname@example.com",
                "test.email@domain.co.jp",
                "username@domain.co.uk",
                "name@domain.co.kr",
                "user@domain.co.in",
                "email@sub.domain.biz",
                "contact@domain.jobs",
                "firstname-lastname@example.com",
                "valid_123@domain.com",
                "user.name+tag@domain.co.uk",
                "username@sub.domain.travel",
                "special_chars@example.io",
                "customer-service@domain.tech",
                "info@nonprofit.org",
                "firstname_lastname@domain.co",
                "email@domain.guru",
                "admin@sub.example.net",
                "test@domain.solutions",
                "firstname.lastname@domain.us",
                "user@domain.company",
                "na%me@domain.expert",
                "support@domain.firm",
                "contact@domain.store",
                "email@domain.agency",
                "person@domain.education",
                "user@domain.center",
                "user@domain.pro",
                "name@domain.club",
                "person@domain.media",
                "user@domain.tools",
                "firstname_lastname@domain.estate",
                "example@domain.tech",
                "test@domain.events",
                "person@domain.services",
                "contact@domain.site",
                "info@domain.community",
                "sales@domain.shop",
                "service@domain.holdings"
            };

            // Create an instance of the Program class
            Program pr = new Program();

            // Iterate through each email address in the array
            foreach (var email in emails)
            {
                // Check if the email is valid using the IsValidEmail method
                if (pr.IsValidEmail(email))
                {
                    //Reset th console txt color
                    Console.ResetColor();
                    // Print if the email is valid
                    Console.WriteLine(string.Format("Valid: {0}", email));
                }
                else
                {
                    //Set console text output color for invalid emails
                    Console.BackgroundColor = ConsoleColor.Red;
                    // Print if the email is invalid
                    Console.WriteLine(string.Format("Invalid: {0}", email));
                }
            }
            // Wait for user input to close the console window
            Console.ReadLine();
        }

        /// <summary>
        /// Method to check whether an email is valid or not
        /// </summary>
        /// <param name="email">A string containing the email address to validate</param>
        /// <returns>True if the email is in a valid format, otherwise false</returns>
        bool IsValidEmail(string email)
        {
            // Regular expression pattern for validating email addresses
            string pattern = @"^[\w.+-]+@[\w-]+\.[\w.-]+$";
            // Create a Regex object
            Regex regex = new Regex(pattern, RegexOptions.Multiline);
            // Use the IsMatch method to validate the email address
            return regex.IsMatch(email);
        }
    }
}
```

## Output :

![How to validate email in C#?](https://www.mindstick.com/interviewquestion/4980283b-16a0-4ce2-ba9e-b02891ada53d/images/f6809f67-cfc3-479d-96a1-6d7b895e247b.png)


---

Original Source: https://www.mindstick.com/interview/33892/how-to-validate-email-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
