Forum Moderators: open

Message Too Old, No Replies

Validate at least one checkbox has been checked

Form validation of checkboxes

         

pops

3:11 am on Jul 7, 2005 (gmt 0)

10+ Year Member



I have a form that has 18 checkboxes. I would like to verify that at least one of them has been checked. The checkboxes use the same form name. I know that by doing this, internally an array is setup with the 18 values. I also know that checkboxes actually are boolean true / false. My challenge, I don't know how to access the field that sets the true / false value. Can someone help me.

Rambo Tribble

3:35 am on Jul 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, forms don't support arrays, as such. It is usually server languages that interpret them as such. With JavaScript you will need to build the array by doing something like:

var ckbx_arr=document.getElementsByName('check_box_name');

You can then loop the array testing the "checked" property, something like:

var ckbx_arr_ln=ckbx_arr.length;
for(var i=0;i<ckbx_arr_ln;i++){
if(ckbx_arr[i].checked)return true;
}
return false;

Rambo Tribble

3:21 pm on Jul 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To respond to your sticky mail, my favorite reference is Danny Goodman's Dynamic HTML, on O'Reilly. Otherwise, the W3C's site is pretty good, for example: [w3.org...]

Generally it is preferred that you keep your follow-up qestions and comments in the thread, so others can gain from them, as well.