In this article, I have explained how you can implement email validation in asp.net 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="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label  id="lblEmail" Text="Email Address:" AssociatedControlID="EmailAddress" runat ="server" />
    <asp:TextBox
        id="EmailAddress"
        Runat="server" />

    <asp:RegularExpressionValidator
        id="regEmail"
        ControlToValidate="EmailAddress"
        Text="Enter valid email id"
        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 
        Runat="server" />    
    
    <p></p>
    
    <asp:Button
        id="btnSubmit"
        Text="Submit"
        Runat="server" />
    
  
        </div>
    </form>
</body>
</html>

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

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

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.

validate-email-address-using-egularexpressionvalidator-min.gif

You might also like to read

Create Captcha in asp.net 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)