Click to See Complete Forum and Search --> : validating multiple radio buttons


IndyB
07-14-2003, 08:57 AM
I've got the following code that I though would work, but I must be missing something. The form has multiple radio buttons and multiple submit buttons. Regardless of which submit button is selected, I need to check and make sure that one of the radio buttons is selected.

Here's the HTML:
<tr>
<td colspan="3"><h5>Search for Claims Exceptions by:<br>
</h5></td>
</tr>
<tr>
<td width="24%" bgcolor="#ececec">Certificate #:</td>
<td width="9%" align="center" bgcolor="#ececec"><input type="radio" name="radio" value="cert"></td>
<td width="67%" rowspan="4" align="center" valign="bottom">
<table width="60%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td colspan="2">View Results By:</td>
</tr>
<tr>
<td width="50%"><input type="button" value="Un-Reviewed" onClick="this.form.action='default.cfm?view_by=un_revd';this.form.submit();"></td>
</tr>
<tr>
<td><input type="button" value="Reviewed" onClick="this.form.action='default.cfm?view_by=revd';this.form.submit();"></td>
</tr>
<tr>
<td><input type="button" name="Submit" value="All" onclick="this.form.action='default.cfm?view_by=all';this.form.submit();"></td>
</tr>
</table>

</td>
</tr>
<tr>
<td bgcolor="#ececec">Policy:</td>
<td align="center" bgcolor="#ececec"> <input type="radio" name="radio" value="policy"></td>
</tr>
<tr>
<td bgcolor="#ececec">Sub-Group:</td>
<td align="center" bgcolor="#ececec"> <input type="radio" name="radio" value="subgroup"></td>
</tr>
<tr>
<td bgcolor="#ececec" nowrap>Century #:</td>
<td align="center" bgcolor="#ececec"> <input type="radio" name="radio" value="century"></td>
</tr>
</form>

Here's the JavaScript:
// JavaScript Document
function valbutton(thisform) {
myOption = -1;
for (i=0; i<thisform.radio.length; i++) {
if (thisform.radio[i].checked) {
myOption = i;
}
}
if (myOption == -1) {
alert("You must select a search option.");
return false;
}

// place any other field validations that you require here
thisform.submit(); // this line submits the form after validation
}

olerag
07-14-2003, 09:26 AM
1. Actually it appears in your html code that you have 4 radio buttons assigned to one radio group.
2. The 3 buttons you've included (performing submits) doesn't call your "valButton()" function. I don't see where your validation function is ever being called.

Which event(s) would you like to initiate your form validation??

You can:
a) Create a "valForm()" (your "main") validation function.
b) From this main validation function, call various other sub-validation functions (such as your radio group selections), ensuring you return the desired results.
c) If everything is validated, submit the results from your "main" validation function, otherwise display the appropriate error message(s).
d) Each of your buttons should, via the "onClick" event. call this main validation routine.