Checkbox Form Validation
Posted: Tue Jun 07, 2011 11:00 am
Hi guys,
I am trying to write some form validation code to check that at least one checkbox is checked when an HTML form is submitted.
The problem is that the number of check boxes is dynamically generated and so I do not know how many checkboxes there are. The variable name of each checkbox is dynamically created as follows:
rec_con_assign_1, rec_con_assign_2, rec_con_assign_3... and so on.
This is what I have written:
It does not work though, I suspect because I write in PHP and so I am sure there is just an error in my code?
Thanks in advance,
Rob.
I am trying to write some form validation code to check that at least one checkbox is checked when an HTML form is submitted.
The problem is that the number of check boxes is dynamically generated and so I do not know how many checkboxes there are. The variable name of each checkbox is dynamically created as follows:
rec_con_assign_1, rec_con_assign_2, rec_con_assign_3... and so on.
This is what I have written:
Code: Select all
var flag=0;
var counter=1;
while (rec_con_assign_[counter]){
if(form.rec_con_assign_[counter].checked){
flag++;
}
counter++;
}
if (flag == 0){
alert ("You must check one and only one checkbox!");
return false;
} Thanks in advance,
Rob.