troubleshooting Question

How to implement a checkboxlist validator checking for checbox checked

Avatar of TechIsaac
TechIsaacFlag for United States of America asked on
* Advanced Custom FieldsASP.NETVisual Basic.NET
2 Comments1 Solution78 ViewsLast Modified:
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
Paul Jackson
Software Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2011

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros