Avatar of Isaac Johnson
Isaac Johnson
Flag for United States of America

asked on 

How to implement a checkboxlist validator checking for checbox checked

I'm trying to include a custom checkboxlist validation to produce an error message if none of the boxes
have been checked.  I've tried to incorporate into my code but to this date and time nothing
seems to want to work.  My most recent try is seen below which is a snippet from my code.

I need direction to something that will work.

<td width="35px" class="pCheck" >
                                        <asp:CheckBoxList ID="cblItems" runat="server" enabled="true" AutoPostBack="true" CellPadding="0"
                                            CellSpacing="0" class="cblItems" RepeatColumns="2" RepeatDirection="Vertical"
                                            RepeatLayout="Table" value="item_name" CausesValidation="true">
                                        </asp:CheckBoxList>
                                        <!-- Isaac -->
                                        <asp:CustomValidator ID="Custom1"
                                                                                     
                                          ClientValidationFunction="CheckBoxValidation"
                                          runat="server"
                                          ErrorMessage="You must select a service or item">
                                                                                   
                                        </asp:CustomValidator>

<script type="text/javascript">
    function CheckBoxValidation(oSrouce, args) {
        var myCheckBox = document.getElementById("cblItems");

        for (i = 0; i < cblItems.length; i++) {
            {
                if (myCheckBox.checked) {

                    args.IsValid = true;
                    break;

                }
                else
                    args.IsValid = false;

            }
           
        }
    }
</script>

When I run this code, the app shows the error message when I check a box, but if I donot check a boxx and submit, the app continues
with recognizing the error - backwards.

I am running out of time to complete this task.

Isaac
* Advanced Custom FieldsASP.NETVisual Basic.NET

Avatar of undefined
Last Comment
Isaac Johnson

8/22/2022 - Mon