In a previous article, I have mentioned Using query string in ASP.NET C# (Web-Forms) but now in this article, I have explained how you can implement email validation in web-forms using regularexpressionvalidator with a simple example. Here are the steps to Validate email address in web-forms.

  • Create a textbox using ASP.NET textbox control
  • Add a regularexpressionvalidator control
  • Create a regex to validate the email address, in the above control
  • Add submit button control, which verify's email on click

Step 1: Create a new project in your Visual Studio, by navigating to File-> Project -> Select "Web" from left pane and "ASP.NET web application" from right pane, Click "OK"

Select "Empty" project Template and check "Web-Forms" for "Add folders and core reference", click OK

Step 2: Create a new .aspx file, right-click on Project name -> Select "Add"-> Select "New Item" -> Select "Web-Form" from right pane, give a name "Default.aspx" and click Ok.

Step 3: Use the code below to validate email id using regex

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="EmailValidator.Default" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
            <asp:Label  id="lblEmail" Text="Email Address:" AssociatedControlID="EmailAddress" runat ="server" />
        Runat="server" />

        Text="Enter valid email id"
        Runat="server" />    
        Runat="server" />

In the above code, RegularExpressionValidator has the following properties.

  • ControlToValidate – ID of the TextBox control, which needs to be validated.
  • ValidationExpression – The Regular Expression for validating the TextBox content. In this example, the Email Address regular expression is used.
  • ErrorMessage – The Error Message to be displayed when Validation fails.

Although, we have used an asp regex validator to confirm input, just ensure you wrap your code-behind method with an if(IsValid) clause in case your javascript is bypassed.

Step 4:  Enable WebForms UnobtrusiveValidationMode, in web.config, use the below code

    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />

Inside "<configuration>", otherwise you might get error "ASP.NET Web-Form Error "WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive)."

That's it, we are done, build your project and run it in your browser.


In case you want to validate email using from code-behind in C#, you can do it using C# and Regex like this

string pattern = @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z";
		//check first string
	   if (Regex.IsMatch(EmailId1 , pattern))
		   //email is valid, do something

you can fine-tune it by checking MX Records also using DNSClient Nuget package. and check MX Record of email as below

// for example,
// domain =
// mail =
public bool ValidateMXRecord(string domain, string mail)
            var lookup = new LookupClient(IPAddress.Parse(""), IPAddress.Parse(""));
            lookup.Timeout = TimeSpan.FromSeconds(5);
            var result = lookup.Query(domain, QueryType.MX);

            var records = result.Answers;

            if (records.Any())
                return true;
                return false;

Hope it helps, thanks.

You might also like to read

Create Captcha in with refresh button (Code with example)

Understanding ASP.NET Gridview control with an example

Search (Filter) Records in ASP.NET GridView with Textbox (Highlighting searched term)

Email Address Validation in C# (With and without Regex)

Using query string in ASP.NET C# (Web-Forms)

Verify email address exists or not? (With email id validation in C#)