Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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.

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 ClientValidationFunction.
Step 4: Create a button control to submit.

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 exampletitle>
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>


This post first appeared on ASPArticles, please read the originial post: here

Share the post

ASP.Net CustomValidator control to check valid E-Mail

×

Subscribe to Asparticles

Get updates delivered right to your inbox!

Thank you for your subscription

×