How To Validate CheckBox with CustomValidator

The CustomValidator control allows you to create a validation control with customized validation logic. For example, you can create a validation control that checks whether the checkbox is checked or not .

To validate the checkbox with The CustomValidator control Use the ClientValidationFunction property to specify the name of the client-side validation script function associated with the
CustomValidator control. Since the script function is executed on the client, the function must be in a language that the target browser supports, such as JavaScript.

The following example demonstrates how to use The CustomValidator control to validate the checkbox :

*Add this script inside the <head> tag
<script type="text/javascript">
function ValidateCheckBox(source,args)
{
    if (document.getElementById('<%=CheckBox1.ClientID%>').checked==true)
    args.IsValid=true;
    else
    args.IsValid=false;
}
</script>

*Add this code in your aspx page
<div>
    <asp:CheckBox ID="CheckBox1" runat="server" Text="You must Agree" />
    <asp:CustomValidator ID="CustomValidator1" runat="server" Display="Dynamic"
    ToolTip="CheckBox must be checked!" ClientValidationFunction="ValidateCheckBox">*
    </asp:CustomValidator>
    
    <br />
           
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</div>

1 comments:

Anonymous said...

can i validate checkbox using compare validator?

Post a Comment