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:
Have you found this post helpful?
If so, I'd appreciate if you could indicate so by pressing the Google Plus button below.
<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!
Related posts:
23 Comments to “Using JavaScript to validate one or more grouped checkboxes”
Post comment
Recommended Services
Recent Posts
- Fantastic new corporate themes for WordPress
- Vistaprint offers FREE t-shirts, too!
- 80+ “Your Ad Here” 125 x 125 banners
- 5 Minute Long Tail SEO Drill: More Traffic, Better SERPs
- iPhone and iPod Touch app statistics: OS adoption, purchasing rates
Recent Comments
- Neerav on Using JavaScript to validate one or more grouped checkboxes
- Jacob on Using JavaScript to validate one or more grouped checkboxes
- kaify on Using JavaScript to validate one or more grouped checkboxes
- JC Goldenstein on How To: Cloak your Affiliate Links for Free in Under 3 Seconds
- Bail Bonds Los Angeles on Amazon Web Services on Rails
Categories
- .net
- acoustic guitar
- affiliate marketing
- ajax
- amazon associates
- blogging
- books
- business ideas
- c#
- code igniter
- dealdotcom
- google adsense
- google adwords
- internet marketing
- iPhone
- javascript
- leadership
- make money online
- mortgage goal
- msn adcenter
- networking
- personal development
- php
- ppc
- ruby
- ruby on rails
- seo
- text-link-ads
- web development
- yahoo search marketing





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
Maruthi says:
It was really helpful.
And quick way to get things done.
thanks a lot
Richa Arora says:
it was really very helpful ..thanks a lot
hema says:
It is really very useful and saved a lot of time.
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!
Gaurav Harjai says:
Thanx a lot Buddy.
it was really very helpful.
Sundaram says:
I was working on the problem for 2 hrs and it would have gone more , if i didnt see this
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.
nie says:
thx.. really help me a lot…
kapil says:
hai,
really helpful.thx a lot
Peter says:
thanks! got me out of a real hole.
love your work!
Boltthrower says:
Excellent. Exactly that i have been looking for… Thanks
Harv Greenberg says:
Bless you. Killed 2 hours on this puppy before figuring out what the unerlying problem was and searching for a soljtion. Thanks.
Halesh.GC says:
Hi,
Thanks a lot…Really very good docs for javascript.
Thanks and Regards
Halesh
Arti says:
Many Thanks !!! Saved a lot of my time :)
Warm regards
Arti
Prakash says:
Exactly that i have been looking for… Thanks …..Thanks a lot .
sriddhar says:
This is fine when the checkbox are named as you did. The problem arises when it is named something like parts[] which is often required in php.
Head
Neck
Arm
Leg
How do you validate then?
Jools says:
Works perfectly thanks.
@sriddhar: I’ve used this with php for a chkbox named img[], I just use img and it’s great.
Dimitri says:
Thank you for posting this – very useful.
Natalya Murphy says:
I was trying to figure out why a single-item radio group wouldn’t validate on a form, and you provided the exact answer I needed. THANK YOU for ending several hours’ worth of frustration.
kaify says:
thanks buddy. it is time saving.
Jacob says:
Works perfectly. Thank you so much!
Neerav says:
Awesome … thanks a bunch.