Web Hosting Talk







View Full Version : javascript validation of checkboxes


szarwell
11-09-2005, 03:10 PM
I have a list of x number of checkboxes on a page (x can vary), when the user submits the form i need to check to see if, for example, checkboxes number 1,3,5,8 are checked, and no others are. FYI, this is used as a multiple choice select all that apply quiz. For usability sake, I would like to be able to easily change the list of correct answers. How do I do this? Am I on the right track below?

function checkBoxes()
{
var correct_choices = new Array(1,3,5,8)
var rightorwrong = "You are correct!"

for(i=0;i<document.form1.choices.length;i++)
{
if it's a checkbox listed in the correct_choices array, verify that it is checked
if it's not, verify it is not checked
if any element is incorrect, set the rightorwrong message to "Please try again"

}
alert(rightorwrong)
}