I'm having some trouble validating radio groups. I thought this would be simple enough but it's not turning out that way. The form is rather long so I have it broken down into sections, each in it's own div. The form shows the first 11 inputs and when you click next it hides the first div and shows the second and so on until you get to the final div that actually submits the form.
There are 11 radio groups for each section, each with 5 inputs. 1 (only one) of the 5 must be selected.
<td align="center"><input type="radio" name="radio2" id="rg2" value="1" /></td>
<td align="center"><input type="radio" name="radio2" id="rg2" value="2" /></td>
<td align="center"><input type="radio" name="radio2" id="rg2" value="3" /></td>
<td align="center"><input type="radio" name="radio2" id="rg2" value="4" /></td>
<td align="center"><input type="radio" name="radio2" id="rg2" value="5" /></td>
I'm not as well versed in Javascript as I would like to be so needless to say I've tried many different ways to validate and none of them really working. The closest I've come to getting it to work is by using this generic script I found on the net.
function checkRadio (frmName, rbGroupName) {
var radios = document[frmName].elements[rbGroupName];
for (var i=0; i <radios.length; i++) {
if (radios[i].checked) {
$('first').hide();
$('second').show();
}
}
alert("You misssed one of the questions");
}
The problem I'm having with this is that it does what I want and displays the alert when you don't select one of the radios, but when you do select one of the radios it lets you continue to the next page but still displays the alert.
The second issue with this is that I'm passing the form and input id's into the function like so:
<input type="button" onclick="checkRadio('getScore','rg1');" value="Next" />
This is fine for radio group 1, but what do I do for #'s 2 - 11?
There must an easier way to go about this. It would be much appreciated if someone could help get me pointed in the right direction.