Skip to main content

Validate CheckBox using Custom Validator and jQuery


 Here is an example on how to validate ASP.Net Web Forms checkbox on client side using custom validator and jQuery.

First you need to have check box and custom validator on the aspx page.

<asp:CheckBox ID="cbTest" runat="server" ValidationGroup="vgValidate" />
<asp:CustomValidator ID="cvTest" runat="server" ClientValidationFunction="validateCheckBox" ErrorMessage="Please check" ValidationGroup="vgValidate"></asp:CustomValidator>


Custom validator is set to call jJavaScript function validateCheckBox, so on the same page you need to have that function defined.

<script type = "text/javascript">
        function validateCheckBox(sender, args) {
            if ($('#<%=cbTest.ClientID%>').is(":checked")) {
                args.IsValid = true;
            } else {
                args.IsValid = false;
            }
        }
    </script> 

Now add the button with the same validation group and on button click checkbox will be validated on client side.

Comments

Popular posts from this blog