Breaking News

LightBlog

Wednesday 9 March 2016

Code for Validating Checkbox in asp.net with required field validator

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.


<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>