INCLUDE_DATA

Article written

  • on 25.08.2005
  • at 08:07 PM
  • by Rory

Using JavaScript to validate one or more grouped checkboxes 16

Aug25

It’s a common task to validate a form with JavaScript upon form submission. And validating whether or not at least one checkbox in a checkbox group is checked is often a basic requirement of this task. But, if you’re form is created dynamically and if the number of checkboxes in a checkbox group can vary from 1 to more than 1, then the JavaScript needed to iterate over the checkboxes is slightly different.

Say you have an HTML form like this:

<form name="frm" method="post">
   <input type="checkbox" name="parts" value="1" />Head<br />
</form>

Then, using JavaScript, you can check whether the checkbox is checked like so:

var isChecked = document.frm.parts.checked;

Now, imagine that your form has more than one checkbox in the same checkbox group:

<form name="frm" method="post">
   <input type="checkbox" name="parts" value="1" />Head<br />
   <input type="checkbox" name="parts" value="2" />Neck<br />
   <input type="checkbox" name="parts" value="3" />Arm<br />
   <input type="checkbox" name="parts" value="4" />Leg<br />
</form>

Then, the JavaScript code changes a little bit, as now you have to iterate over each of the checkboxes.

var isChecked = false;

for (var i = 0; i < document.frm.parts.length; i++) {
   if (document.frm.parts[i].checked) {
      isChecked = true;
   }
}

But. if you tried using the 2nd chunk of JavaScript on the first chunk of HTML, you’ll get a JavaScript error. This is because if you only have one checkbox, there’ll be no array to iterate over. So, you need to do a quick check before hand to see if there is more than one checkbox. If so, then iterate; otherwise, don’t.

if (document.frm.parts) {
   if (typeof document.frm.parts.length != 'undefined') {
      for (i = 0; i < document.frm.parts.length; i++) {
         if (document.frm.parts[i].checked) {
            isChecked = true;
         }
      }
   } else {
      isChecked = document.frm.parts.checked;
   }
}

This code first checks to see that there exists at least one checkbox. Then, it checks whether the length attribute of the parts variable is defined or not. If it is defined, then we know there is more than one checkbox. Otherwise, there is only one.

And there ya have it!

subscribe to comments RSS

There are 16 comments for this post

  1. Matt Cocker says:

    Hi and many thanks.
    I had just found a problem in a dynamic list of checkboxes that fails when there is only one box.
    This answers the question as to why and gives the perfect solution, look slike it even works if there were no check boxes at all for some reason.
    Thanks again.
    Matt Cocker

  2. Maruthi says:

    It was really helpful.
    And quick way to get things done.
    thanks a lot

  3. Richa Arora says:

    it was really very helpful ..thanks a lot

  4. hema says:

    It is really very useful and saved a lot of time.

  5. DAR says:

    Many thanks. I have a dynamic list of checkboxes and this has been driving me nuts. I was working under the assumptin that, even if only a single checkbox line gets generated, I could process it as an array of size 1 in my onClick JavaScript function. Nice explanation, nice easy-to-understand example, nice efficient code – well done!

  6. Gaurav Harjai says:

    Thanx a lot Buddy.

    it was really very helpful.

  7. Sundaram says:

    I was working on the problem for 2 hrs and it would have gone more , if i didnt see this

  8. aprian says:

    Thx’s a lot, it really helpfull.

    I never realized before it will give an error if the checkbox is only one for this : document.frm.parts.length.

  9. nie says:

    thx.. really help me a lot…

  10. kapil says:

    hai,

    really helpful.thx a lot

  11. Peter says:

    thanks! got me out of a real hole.
    love your work!

  12. Boltthrower says:

    Excellent. Exactly that i have been looking for… Thanks

  13. Bless you. Killed 2 hours on this puppy before figuring out what the unerlying problem was and searching for a soljtion. Thanks.

  14. Halesh.GC says:

    Hi,

    Thanks a lot…Really very good docs for javascript.

    Thanks and Regards

    Halesh

  15. Arti says:

    Many Thanks !!! Saved a lot of my time :)

    Warm regards
    Arti

  16. Prakash says:

    Exactly that i have been looking for… Thanks …..Thanks a lot .

Please, feel free to post your own comment

* these are required fields