ASP.Net CheckBox Required Validation using Custom Validator and JavaScript
 
Author:
Filed Under: ASP.Net  |  JavaScript  |  ASP.Net Validators
Published Date: Aug 10, 2011
Views: 2716
 

Abstract: Here Mudassar Ahmed Khan has explained how to do validate ASP.Net CheckBox client side using ASP.Net Custom Validator and JavaScript

Comments:  0

 

In this article I will explain how to validate ASP.Net Checkbox control using ASP.Net Custom Validator and JavaScript
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type = "text/javascript">
        function ValidateCheckBox(sender, args) {
            if (document.getElementById("<%=CheckBox1.ClientID %>").checked == true) {
                args.IsValid = true;
            } else {
                args.IsValid = false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:CheckBox ID="CheckBox1" runat="server" />
    <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Required" ClientValidationFunction = "ValidateCheckBox"></asp:CustomValidator><br />
    <asp:Button ID="Button1" runat="server" Text="Submit"/>
    </form>
</body>
</html>
 
Explanation
Above I have placed an ASP.Net Custom Validator besides the ASP.Net Checkbox control. For the ASP.Net Custom Validator I have specified the ClientValidationFunction property to a JavaScript function ValidateCheckBox which validates the whether the checkbox is checked or not.

Screenshot
Validate ASP.Net CheckBox using JavaScript and Custom Validator
 

The above code has been tested in the following browsers

Internet Explorer  FireFox  Chrome  Safari  Opera 

* All browser logos displayed above are property of their respective owners.


Downloads
You can download the sample source code using the download link provided below.
ValidateASP.NetCheckBox.zip








Related Articles



Comments

No comments have been added to this article.

Add comments

You can add your comment about this article using the form below. Make sure you provide a valid email address
else you won't be notified when the author replies to your comment

Please note that all comments are moderated and will be deleted if they are
  • Not relavant to the article
  • Spam
  • Advertising campaigns or links to other sites
  • Abusive content.
Please do not post code, scripts or snippets.

Name*: Required
Email*: Required
Comment*: Required
Security code*: CaptchaInvalid Security Code
  Submit