Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

CustomValidator control in asp.net - Part 48

Suggested Videos
Part 45 - Rangevalidator
Part 46 - CompareValidator
Part 47 - RegularExpressionValidator

CustomValidator control in asp.net

CustomValidator control allows us to write a method with a custom logic to handle the validation of the value entered. If none of the other validation controls, serve our purpose, then the CustomValidator can be used.



Just like any other validation control, CustomValidator can be used for both client and server side validation. Client side and server side validation functions, needs to be written by the developer, and associate them to the custom validator control.

The following are the properties that are specific to the CustomValidator control
ClientValidationFunction - Specifies the name of the client side validation method.
ValidateEmptyText - Specifies whether the validator validates the control, when the text of the control is empty. By default this property is false, and both the client side and server side validation functions will not be invoked, if the associated input control is empty.

Events specific to the CustomValidator control
OnServerValidate - Specifies the name of the server side validation method.



HTML of the aspx page: This example checks if the entered number is even. ValidateEmptyText property of the CustomValidator control, is set to true. So the validation is also triggered when the textbox is empty.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script language="javascript" type="text/javascript">
        // Client side validation function to check if the number is even. 
        function IsEven(source, args) 
        {
            if (args.Value == "") 
            {
                args.IsValid = false;
            }
            else 
            {
                if (args.Value % 2 == 0) 
                {
                    args.IsValid = true;
                }
                else 
                {
                    args.IsValid = false;
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="font-family: Arial">
        <table>
            <tr>
                <td>
                    <b>Please enter a positive even number</b>
                </td>
                <td>
                    <asp:TextBox ID="txtEvenNumber" runat="server"></asp:TextBox>
                    <asp:CustomValidator ID="CustomValidatorEvenNumber" runat="server"
                        ForeColor="Red" 
                        ErrorMessage="Not an even number"
                        OnServerValidate="CustomValidatorEvenNumber_ServerValidate"
                        ControlToValidate="txtEvenNumber"
                        ClientValidationFunction="IsEven"
                        ValidateEmptyText="true">
                    </asp:CustomValidator>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="btnSubmit" runat="server" Text="Save" 
                        onclick="btnSubmit_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblStatus" runat="server" Font-Bold="true"></asp:Label>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Code-Behind page code: Set EnableClientScript to true, to test the server side validation method.
protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        lblStatus.Text = "Data Saved";
        lblStatus.ForeColor = System.Drawing.Color.Green;
    }
    else
    {
        lblStatus.Text = "Validation Error! Data Not Saved";
        lblStatus.ForeColor = System.Drawing.Color.Red;
    }
}

protected void CustomValidatorEvenNumber_ServerValidate(object source, ServerValidateEventArgs args)
{
    if (args.Value == "")
    {
        args.IsValid = false;
    }
    else
    {
        int Number;
        bool isNumber = int.TryParse(args.Value, out Number);
        if (isNumber && Number >= 0 && (Number % 2) == 0)
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
}

2 comments:

  1. amazing but i have an issue

    first of all thank you

    the thing is

    client side validation doesn't validate empty string on first attempt however after invoking the validator at least once it works as expected ' I'm I losing something here '

    ReplyDelete
    Replies
    1. you have to use required field validator simultaneously

      Delete

It would be great if you can help share these free resources