how to validate two seperate checkbox groups

Posted on 2012-08-28
Last Modified: 2012-08-29
Hello Experts,

I have a page that lists email recipients in 2 checkbox groups. List 1 is a static set of email addresses (simple to validate), and List 2 is created via a DB query, so the list is dynamic (some difficulty here).

My question comes in two parts.

1) Once the submit button is clicked I need to validate if ANY boxes in EITHER group has been checked. A false will generate a "you have to select a recipient" error. I just need to know the code for "if nothing from List 1 or List 2 has been selected..."

2) List 2 is dynamic. I need to know how to code javascript to be able to handle changes in List 2 to be able to work properly with the first question above.

Thank you in advance.
Question by:CementTruck
    LVL 10

    Expert Comment

    I've produced two helper routines so that you can easily write the code you want. For this form:
    <form id=form onSubmit="return onSubmit(this)">
    <input type=checkbox name="box1" value="1">One
    <input type=checkbox name="box1" value="2">Two
    <input type=checkbox name="box1" value="3">Three<br>
    <input type=checkbox name="box2" value="a">A
    <input type=checkbox name="box2" value="b">B
    <input type=checkbox name="box2" value="c">C<br>
    <input type=submit name="submit">
    function element(theForm,name)
      var elem = new Array()
      for each (e in theForm.elements)
        if ( == name)
      if (elem.length == 0) return null
      if (elem.length == 1) return elem[0]
      return elem
    function isChecked(theForm,name)
      var isChecked = false
      for each (var box in element(theForm,name))
        isChecked |= box.checked
      return isChecked
    function onSubmit(theForm)
      if (!isChecked(theForm,"box1" && !isChecked(theForm,"box2"))
        alert("You must select a recipient")
        return false
      return true

    Open in new window

    LVL 63

    Expert Comment

    Hello CementTruck,

    to be able to address your second question part we need one sample html output as seen in browser html source when the page is displayed.
    Can you post an example?
    LVL 10

    Accepted Solution

    Zvonko, the code sample I posted works regardless of how many choices are available for each checkbox.
    LVL 63

    Expert Comment

    @CementTruck: can you post some html page source example?

    Author Closing Comment

    Sorry guys. Got hung up doing some other project and couldn't get back to you soon enough.

    @Zvonko - I got tdlewis' code to work, and he is correct regarding the quantity of boxes. Thank you for your time.

    @tdlewis - Thank you for your code.

    Cheers all!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    In my daily work (mainly using, I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
    This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now