Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Validate that at least one checkbox is checked

Reply
Thread Tools

Validate that at least one checkbox is checked

 
 
Zvonko Biškup
Guest
Posts: n/a
 
      03-13-2007
Hi!

I have 5 checkboxes with the same name:
<input type="checkbox" name="check" value="XYZ" />
<input type="checkbox" name="check" value="ZXY" />
<input type="checkbox" name="check" value="MHG" />
<input type="checkbox" name="check" value="IOJ" />
<input type="checkbox" name="check" value="BGT" />

How to check if at least one is checked?

Thanks
Zvonko


 
Reply With Quote
 
 
 
 
Ivo
Guest
Posts: n/a
 
      03-13-2007
"Zvonko Biškup" wrote
> I have 5 checkboxes with the same name:
> <input type="checkbox" name="check" value="XYZ" />
> <input type="checkbox" name="check" value="ZXY" />
> <input type="checkbox" name="check" value="MHG" />
> <input type="checkbox" name="check" value="IOJ" />
> <input type="checkbox" name="check" value="BGT" />
>
> How to check if at least one is checked?


Assuming they are inside a form named "f", try something like:

var els = document.forms.f.elements;
var i = els.length;
var flag = false;
while( i-- ) {
if( els[i].name==='check' && els[i].checked ) {
flag = true; break;
}
}
alert( flag ? 'There is a checkbox checked' : 'Try again.' );

hth
ivo
http://4umi.com/web/javascript/


 
Reply With Quote
 
 
 
 
-Lost
Guest
Posts: n/a
 
      03-13-2007
"Zvonko Biškup" <(E-Mail Removed)> wrote in message
news:45f64915$(E-Mail Removed)-net.net...
> Hi!
>
> I have 5 checkboxes with the same name:
> <input type="checkbox" name="check" value="XYZ" />
> <input type="checkbox" name="check" value="ZXY" />
> <input type="checkbox" name="check" value="MHG" />
> <input type="checkbox" name="check" value="IOJ" />
> <input type="checkbox" name="check" value="BGT" />
>
> How to check if at least one is checked?


document.forms['form_name'].elements['check'] will give you a nodeList of your checkboxes.
You can then iterate them with a loop checking for the "checked" value to be true. If
none are true, nothing is checked.

-Lost


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
CheckBox Checked=false when checked in DataList yurps ASP .Net 1 02-29-2008 01:08 PM
Ensure at least one checkbox or input element is checked or not empty DoomedLung Javascript 2 09-28-2006 05:33 PM
how do I check if at least one of several checkboxes has been checked? NotGiven Javascript 5 02-12-2004 09:33 PM
Checkbox - database checkbox, if checked gives value of 1 .. how to sum ? randy Perl Misc 13 11-01-2003 05:39 AM



Advertisments