Validate Email id in C# Desktop Application

In Email Id, the text after the @ symbol generally refers to the server that is sending the Email, and the part before the @ symbol is a unique identifier according to that mail server. In this C# example let’s create a simple desktop application to validate email id and print validation errors in a popup.

Validate Email id in C#

User will enter Email Id/Address in TextBox and when the Button is clicked, the Email Address will be tested against the Regular Expression and if invalid, a Message Box will be displayed. we can make an application in C# in which we can keep a Text box that accepts only valid Email addresses and rejects or show an error message while entering the invalid type of Email Id.

Create Desktop app to Validate Email id in C#

First Open your Visual studio > click on New Project in Visual C# Windows Application. >Name the project. > Click on Ok.

> take the first Form the default name of the Form will be Form1 I have changed its name to Email Id Validation you can also take the name as per your choice. > Then take one Lable, one TextBox control and one Button control from the toolbox on it.

Now give the label text as Enter Your Email Address and Button1 text as Check.

Step 2:

First of all add the following namespace on the top of all coding part.

using System.Text.RegularExpressions;

Step 3:

Create a method.

private static Regex email_validation()
            string pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
                + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
                + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";
            return new Regex(pattern, RegexOptions.IgnoreCase);

Step 4:

Initialize the method that we have created.

static Regex validate_emailaddress = email_validation();

Step 5:

Now double click on the Check Button and write the below code there.

private void button1_Click(object sender, EventArgs e)
            if (validate_emailaddress.IsMatch(textBox1.Text) != true)
                MessageBox.Show("Invalid Email Address!", "Invalid", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                MessageBox.Show("Email Address is valid.");


When the button will be clicked, the Email Address from TextBox will be tested against the Regular Expression using the IsMatch function. If the Email Address is found invalid an error message would be displayed using MessageBox.


If the email id you have entered in the textbox does not match then it will show the error message like below.

If it is matched with the regular expression we have given then it will show the result like below.

Here is one more output screenshot of the valid Email address.

So in this way you can also validate a Text box for an Email Id in C#.