Sushma
Sushma

Reputation: 13

Custom Validator Undefined Method Error

I am getting an error during runtime 'cvLeapYear_ServerValidate' is undefined

The validator is defined as

<asp:CustomValidator ID="CustomValidator1" runat="server"
                 ClientValidationFunction="cvLeapYear_ServerValidate"
                 ControlToValidate="txtLeapYear" ErrorMessage="Not a leap year">
</asp:CustomValidator>

The function

protected void cvLeapYear_ServerValidate(object source,ServerValidateEventArgs args)
{
    int year;
    try
    {
        year = Convert.ToInt32 (args.Value);
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
    catch(Exception e)
    {
        args.IsValid = false;
    }
}

is defined in the backend code and following javascript code is defined in aspx webform

<script type="text/javascript">
    function isLeapYear(sender,args)
    {
        year = parseInt(args.Value);
        if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0))
        {
            args.IsValid = true;
        }
        else
        {
            args.IsValid = false;
        }
    }
</script>

and clientValidation function is also set.

Upvotes: 1

Views: 1054

Answers (2)

Shubham B
Shubham B

Reputation: 366

ClientValidationFunction should have the value of a javascript function which is called to validate from client side. You have to call isLeapYear function.

<asp:CustomValidator ID="CustomValidator1" runat="server"
             ClientValidationFunction="isLeapYear"
             ControlToValidate="txtLeapYear" ErrorMessage="Not a leap year">
</asp:CustomValidator>

Your function cvLeapYear_ServerValidate is a server side function which you can call using OnServerValidate attribute.

 <asp:CustomValidator ID="CustomValidator1" runat="server"
             OnServerValidate ="cvLeapYear_ServerValidate"
             ControlToValidate="txtLeapYear" ErrorMessage="Not a leap year">
</asp:CustomValidator>

Upvotes: 0

Anil
Anil

Reputation: 3752

<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="isLeapYear" ControlToValidate="txtLeapYear" ErrorMessage="Not a leap year" OnServerValidate="cvLeapYear_ServerValidate" ></asp:CustomValidator>

Add name of javascript function, as given in above code

Upvotes: 4

Related Questions