ASP.Net CustomValidator control to check valid E-Mail

 Posted By :  ASPArticles
 Posted Date: Jul 29, 2016
 Category :  , ,
 Post Hits : 149

In this article, we are going to learn ASP.Net CustomValidator control. CustomValidator control can be used for both client side and server side validation. We need to write our own custom functions for both client and server side. And finally associate these functions to CustomValidator. So, we are going to validate an E-Mail id using CustomValidator control.

Related Article

In Designer File

Step 1: Create a TextBox control which will take valid E-Mail id as a input.
Step 2: Create a CustomValidator control along with its ControlToValidate, ErrorMessage, ClientValidationFunction and OnServerValidate properties.
Step 3: Associate ClientValidationFunction property with client side JavaScript function and generate event for OnServerValidate.
Step 4: Create a button control to submit.

ASP.Net CustomValidator to check valid E-Mail

Below is the designer code.

        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator1"
        ErrorMessage="Enter Valid E-Mail"
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

Below is the JavaScript checkValidEmail function code.

<head runat="server">
        <title>ASP.Net CustomValidator example</title>
        <script type="text/javascript">
        function checkValidEmail(sender, e) {
        var EmailRegExpression = /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/;
        if (EmailRegExpression.test(e.Value)) {
        return e.IsValid = true;
        else {
        return e.IsValid = false;

In Code-Behind File

Include the below Namespace.

using System.Text.RegularExpressions;

Below is the C# code.

protected void btnSubmit_Click(object sender, EventArgs e)
        if (Page.IsValid)
        // this block is executed when validation is passed
        // save your data
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        var regexExpression = @"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$";
        string inputEmail = args.Value.Trim();
        var match = Regex.Match(inputEmail, regexExpression, RegexOptions.IgnoreCase);
        if (match.Success)
        args.IsValid = true;
        args.IsValid = false;


WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for 'jquery'. Please add a ScriptResourceMapping named jquery(case-sensitive).

If you are getting error like above. Please add ValidationSettings:UnobtrusiveValidationMode in appSettings in web.config file.

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

blog comments powered by Disqus