Question

Here is my code for checkbox list: Data is bounded with Look Up Table

 <asp:CheckBoxList ID="cbl_AppliancePresent" ClientIDMode="Static"
 CssClass="cbl_AppliancePresent"
                     runat="server" DataTextField="Appliance" DataValueField="ApplianceID" RepeatDirection="Horizontal">
                 </asp:CheckBoxList>

Here is the Custom Validator:

 <asp:CustomValidator ID="SpecifyAppliances" ClientIDMode="Static"
  runat="server" EnableClientScript="true" ErrorMessage="Please select
     Vacant-not secure reasons"  Font-Bold="True"
     ClientValidationFunction="ValidateNotSecure" ForeColor="Red" 
     Font-Size="Small"></asp:CustomValidator>

Function:

function ValidateNotSecure(source, args) {

    var options = $('.cbl_AppliancePresent');
    for (var i = 0; i < options.length; i++) {
              if (options[i].checked) {
        EnableValidator('SpecifyAppliances'); 
                   args.IsValid = true;
        return false;
               }
    }
    DisableValidator('SpecifyAppliances');
    args.IsValid = false; }
Was it helpful?

Solution

If u want it to be solved by simply using jquery and without using custom validator then here is the solution.

<body>
    <form id="form1" runat="server">
    <div id="chl1">
     <asp:CheckBoxList ID="CheckBoxList1" runat="server">
            <asp:ListItem>Item1</asp:ListItem>
            <asp:ListItem>Item2</asp:ListItem>
            <asp:ListItem>Item3</asp:ListItem>
            <asp:ListItem>Item4</asp:ListItem>
        </asp:CheckBoxList>
        <asp:Label ID="Label1" runat="server" Text="" ClientIDMode="Static"></asp:Label>

        <asp:Button ID="btncheck" runat="server" Text="Button" ClientIDMode="Static" />

    </div>
    </form>
    <script type="text/javascript">
        $(document).ready(function () {
           $("#btncheck").click(function () {
                var a = 0;
                $(":checkbox").each(function () {
                    if (this.checked) {
                        a = a + 1;
                    }
                });
                if (a == 0) {
                    $("span[id$='Label1']").text('Please select');
                    return false;
                }                           
            });
        });

    </script>
</body>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top