In this article, I have explained how you can implement email validation in web-forms using regularexpressionvalidator with an 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.

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.


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)