Link to home
Start Free TrialLog in
Avatar of Isaac Johnson
Isaac JohnsonFlag 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
ASKER CERTIFIED SOLUTION
Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Isaac Johnson

ASKER

Thanks so much Paul.

Isaac