|
Validation of groups of radio buttons is
straightforward. The following function will validate
for you that one of the radio buttons in a group has
been selected:
All that you need to do to use the above function is to
call it from within your form validation routine
passing it the radio button group that is to be
validated and it will return the value of the button
within the group that is selected or null if no button
in the group is selected.
For example the code that I used in the example form to
perform the radio button validation is as follows:
var btn = valButton(form.group1);
if (btn == null) alert('No radio button
selected');
else alert('Button value ' + btn + '
selected');
This code was included into the function called by an
onclick event attached to the validate (or submit)
button on the form. A reference to the whole form was
passed as a parameter into the function which uses the
"form" argument to refer to the complete
form. To validate the radio button group with the name
group1 we therefore pass form.group1 to the valButton
function.
I said at the start that defining radio buttons and
their validation is the simplest of form fields to
handle. All of the radio button groups that you will
ever need can be handled using the steps that I have
covered here.
|