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
Browser Compatibility
The above code has been tested in the following browsers.
* 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.
Download Code