ASP.Net CustomValidator control to check valid E-Mail

 Print 
 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.

<table>
        <tr>
        <td>
        <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator1"
        runat="server"
        ControlToValidate="txtEmail"
        ErrorMessage="Enter Valid E-Mail"
        ClientValidationFunction="checkValidEmail"
        OnServerValidate="CustomValidator1_ServerValidate">
        </asp:CustomValidator>
        </td>
        </tr>
        <tr>
        <td>
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
        </td>
        </tr>
</table>

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;
            }
        }
        </script>
</head>

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
    }
        else
    {
      
    }
}
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;
    }
        else
    {
        args.IsValid = false;
    }
}

Error

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.

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




blog comments powered by Disqus