Paste this function into the <head> section of your template:
function MandatoryCheckBox(formname, fieldname) {
/* This functions' role in life is to determine whether or not at least one
checkbox or radio button,
has been selected out of a series of checkboxes or radio buttons. */
objRadio = eval("document." + formname + "." + fieldname)
var arrLength = objRadio.length -1;
if (isNaN(arrLength)) { //in case there's only one option
objFocus = objRadio
if(objRadio.checked == true) {
return true;
}
} else { //multiple options
objFocus = objRadio[0]
for(i=0; i<=arrLength; i++) {
if(eval("objRadio" + "[" + i + "].checked") == true) {
return true;
}
}
}
objFocus.focus();
return false;
}
Then, when you submit the form, call the function as many times as needed
(once for each grouping of radio buttons):
function validateForm(formname) {
if(!(MandatoryCheckBox(formname, "WPAvailability"))) {
alert("Please select a value from ... your field label text here...");
return false;
}
if(!(MandatoryCheckBox(formname, "WPProficiency"))) {
alert("Please select a value from ... your field label text here...");
return false;
}
//etc - other groups or other form field validation routines go here...
//don't forget to return true if all the checks have been passed
return true;
}
I submit the form like so:
<form action="someotherpage.cfm" method="post" name="myFormName"
onSubmit="return validateForm(this.name)">
Be sure to assign a name to the form, and note the onSubmit value!
If the form field has not been selected, the form will not be submitted, and
the focus will be set to that specific field. Pretty handy...
Good Luck!
Jeff
-----Original Message-----
From: Matthew Hood [mailto:[EMAIL PROTECTED]]
Sent: Saturday, May 12, 2001 5:44 PM
To: CF-Talk
Subject: Slightly OT: Form Validation Woes
Ok, here is the problem:
I have various sets of radio buttons on a page. 4 sets to a page
actually.
I need a javascript that will validate that a radio button has been
selected
for each of the four sets. Being no good a javascript I am turning to
you for help. ( I know this isn't a cold fusion question per say but
the values are entered into a DB when all is said and done.)
Is there a script that can run through each set and verify that a
selection has been made, without having to manually edit the script
for each page and inputting the names?
Here is a what the radio buttons look like:
<TD>
<cfinput type="Radio" name="WPAvailability" value="5"
tabindex="1">5<br>
<cfinput type="Radio" name="WPAvailability" value="4"
tabindex="2">4<br>
<cfinput type="Radio" name="WPAvailability" value="3"
tabindex="3">3<br>
<cfinput type="Radio" name="WPAvailability" value="2"
tabindex="4">2<br>
<cfinput type="Radio" name="WPAvailability" value="1"
tabindex="5">1<br>
<cfinput type="Radio" name="WPAvailability" value="0" tabindex="6">N/A
</TD>
<TD>
<cfinput type="Radio" name="WPProficiency" value="5" tabindex="1">5<br>
<cfinput type="Radio" name="WPProficiency" value="4" tabindex="2">4<br>
<cfinput type="Radio" name="WPProficiency" value="3" tabindex="3">3<br>
<cfinput type="Radio" name="WPProficiency" value="2" tabindex="4">2<br>
<cfinput type="Radio" name="WPProficiency" value="1" tabindex="5">1<br>
<cfinput type="Radio" name="WPProficiency" value="0" tabindex="6">N/A
</TD>
<TD>
<cfinput type="Radio" name="WPImportance" value="5" tabindex="1">5<br>
<cfinput type="Radio" name="WPImportance" value="4" tabindex="2">4<br>
<cfinput type="Radio" name="WPImportance" value="3" tabindex="3">3<br>
<cfinput type="Radio" name="WPImportance" value="2" tabindex="4">2<br>
<cfinput type="Radio" name="WPImportance" value="1" tabindex="5">1<br>
<cfinput type="Radio" name="WPImportance" value="0" tabindex="6">N/A
</TD>
<TD>
<cfinput type="Radio" name="WPFrequency" value="5" tabindex="1">5<br>
<cfinput type="Radio" name="WPFrequency" value="4" tabindex="2">4<br>
<cfinput type="Radio" name="WPFrequency" value="3" tabindex="3">3<br>
<cfinput type="Radio" name="WPFrequency" value="2" tabindex="4">2<br>
<cfinput type="Radio" name="WPFrequency" value="1" tabindex="5">1<br>
<cfinput type="Radio" name="WPFrequency" value="0" tabindex="6">N/A
</TD>
Any help would be GREATLY appreciated! Thanks.
Matthew Hood
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structure your ColdFusion code with Fusebox. Get the official book at
http://www.fusionauthority.com/bkinfo.cfm
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists