Forum Moderators: open

Message Too Old, No Replies

Intermediate Form Validation, please help..

form validation methods

         

skizm

1:02 pm on Aug 15, 2005 (gmt 0)

10+ Year Member



Hi, I've been having troubles accomplishing two things. How can I put validation checking on a group of checkboxes, to make sure atleast 1 of the boxes are checked, and how do I do the same with a group of radio buttons to make sure atleast one is checked.

Any insight would be appreciated, thank you...

garann

7:40 pm on Aug 15, 2005 (gmt 0)

10+ Year Member



The radio buttons are easy:


var isChecked = false;
var radios = document.getElementsByTagName("myRadioButtons");
for (var i=0;i<radios.length;i++) {
if (radios[i].checked) isChecked = true;
}

Checkboxes are only a little more dificult. You can do the same thing as above, but you'd need a way of creating an array of all your checkboxes. If your form's not dynamic, you can just hardcode them. Otherwise, you might want to put them in a span or other element with a special ID and load your array with

document.getElementById("mySpanForCheckboxes").childNodes
.