Friday, 29 July 2016

ASP.Net CustomValidator control to check valid E-Mail

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

  1. ASP.Net TextBox validation using JavaScript
  2. Simple registration form with client side validation in asp.net
  3. Call JavaScript function from code behind C# file in ASP.Net
  4. Simple registration form with server side validation in asp.net
  5. Show ValidationSummary ErrorMessage in popup in asp.net

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>
Share:

0 comments:

Post a Comment

Email Subscription

Subscribe to our newsletter to get the latest articles directly into your inbox

Blog Archive

BUY FROM AMAZON