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
TechIsaacIT Contracting Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Paul JacksonSoftware EngineerCommented:
try changing your Javascript function as below:

<script type="text/javascript">
    function CheckBoxValidation(oSrouce, args) {
        var myCheckBoxList = document.getElementById("<%=cblItems.ClientID%>");
	var myCheckBoxes = myCheckBoxList.getElementsByTagName("input");
	var isValid = false;

        for (i = 0; i < myCheckBoxes.length; i++) {
        {
                if (myCheckBoxes[i].checked) {

                    isValid = true;
                    break;

                }
        }
	args.IsValid = isValid;
    }
</script>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TechIsaacIT Contracting Author Commented:
Thanks so much Paul.

Isaac
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Advanced Custom Fields

From novice to tech pro — start learning today.