Web Tutorials

Interview Q & A

Code Examples

Utility Tools

ASP.NET Client and Server Side CustomValidator

ASP.NET CustomValidator is used to implement client and server side validation as per requirement for example implement US postal or zip code validation. The CompareValidator control belongs to System.Web.UI.WebControls

Syntax

<asp:CustomValidator ID="txtZipValidation" runat="server" 
ErrorMessage="Valid Zip Code is required (ex: #####)" 
ControlToValidate="txtZip" ClientValidationFunction="validateZipCode"
OnServerValidate="validateZipCode">
</asp:CustomValidator>

ASP.NET CustomValidator Control Property

PropertyDescription
ControlToValidateSpecifies control name which needs to be validate.
DisplaySpecifies behavior of the error message.
EnableClientScriptSpecifies whether or not client-side validation is enabled.
EnabledSpecifies whether or not validation control is enabled or not.
ErrorMessageSpecifies error messageto be display in validation summary.
ForeColorSpecifies error message color.
IsValidIndicate if associate control passes validation or not.
SetFocusOnErrorSet focus on associate control if validation fails.
TextSpecifies text to be display if validation fails.
ValidationGroupSpecifies validation group name.
ValidateUpdate the isvalid propery.
ValidateGetValidationPropertyDetermineing validation property of a control, if it exists.
ClientValidationFunctionSpecifies custom client-side script function name.
ValidateEmptyTextSet whether empty text should validate or not.
ServerValidateSet whether you want server-side validation.

ASP.NET Client And Server Side CustomValidator Example - Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ASP.NET CustomValidator Control Example</title>
<script type="text/javascript">
    function validateZipCode(sender, args) {
        var val = args.Value;
        var regEx = /^[0-9]+$/;

        if (!regEx.test(val)) {
            args.IsValid = false;
        }
        else {
            if (val.length == 5) {
                if (val > 0) {
                    args.IsValid = true;
                }
                else {
                    args.IsValid = false;
                }
            }
            else
                args.IsValid = false;
        }
    }
</script>
</head>
<body>
<form id="Form" runat="server">
<div>
<asp:Label ID="lblTitle" runat="server" Font-Bold="True" Font-Size="Medium" 
Font-Underline="True" Text="ASP.NET CustomValidator Control Example"></asp:Label>
<br /><br />
<asp:Label ID="lblZipCode" runat="server" Font-Bold="True" Font-Size="Small" 
Text="Enter Zip Code:"></asp:Label>
<asp:TextBox ID="txtZip" runat="server"  CssClass="profileFormObject" Width="53"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
ControlToValidate="txtZip" 
ErrorMessage="This is required field, can not be blank."></asp:RequiredFieldValidator>
<asp:CustomValidator ID="txtZipValidation" runat="server" 
ErrorMessage="Valid Zip Code is required (ex: #####)" 
ControlToValidate="txtZip" ClientValidationFunction="validateZipCode"
OnServerValidate="validateZipCode">
</asp:CustomValidator>
<br />
<asp:Button ID="btnSubmit" runat="server" Font-Bold="True" 
Font-Size="Small" Text="Submit" />  
</div>
</form>   
</body>  
</html> 

ASP.NET Client And Server Side CustomValidator Example - Default.aspx.cs

protected void validateZipCode(object source, ServerValidateEventArgs args)
{
	Regex zip = new Regex("[0-9]{5}");
	if (zip.IsMatch(this.txtZip.Text.Trim()))
	{
		args.IsValid = true;  
	}
	else
	{
		args.IsValid = false; 
	}
}

Above example will produce following output

RequiredFieldValidator

RegularExpressionValidator

RangValidator

CompareValidator

CustomValidator

ValidationSummary