Populer Topics

Custom Validator Control

Using Custom validator control we can customize our checking with control we know that validation control check at both server and client side.

But in other control we do not need to create any function. In custom validator we can create own function with the custom validator control.

A Custom validator control provide following format of the function.


A Custom validator control support following property.

Property Description
ClientValidationFunction This property is used to specify client side validation function name.
ValidationEmptyText This is a Boolean property which is specifies when blank text should be validate or not.
OnServerValidate We can specifies the name of server side validation script function to be executed.
ControlToValidate We can specifies the id of control to validate.
ErrorMessage  The text to be display in the validation summery control when validation is fail.

The Example of the Custom validator control is following.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" 
Inherits="Codeplanex.WebForm1" %>

<!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 runat="server">
    <title></title>
    <script type="text/javascript">
    function NumberChecker(ObjectSource,ObjectArgs)
    {
        no = ObjectArgs.Value;
        if (no % 2)
        {
            ObjectArgs.IsValid = true;
        }
        else
        {
            ObjectArgs.IsValid = false;
        }
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox ID="txtNumber" runat="server"></asp:TextBox><br />
        <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
        <asp:CustomValidator ID="CV1" runat="server" ControlToValidate="txtNumber" 
        ErrorMessage="* It Required Only Even Number." 
        ClientValidationFunction="NumberChecker"></asp:CustomValidator>
    </form>
</body>
</html>