Wednesday, May 22, 2013

Validate Checkbox using Asp.Net Validator

In the designer / .aspx:


<asp:CheckBox ID="chkAgree" runat="server" />I agree to the <a href="#" class="link-blue"> Terms of Service</a> and <a href="#" class="link-blue">Privacy Policy</a>

<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="ValidateCheckBox" CssClass="field_validation" ErrorMessage="Please accept our Terms of Service in order to avail our services."                 ></asp:CustomValidator>

and the Javascript code:

<script type="text/javascript">
    function ValidateCheckBox(sender, args) {
       if (document.getElementById("<%=chkAgree.ClientID %>").checked == true) 
          {
            args.IsValid = true;
          } 
          else 
          {
            args.IsValid = false;
          }
       }
</script>

No comments:

Post a Comment