Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Radio Validation Help Needed 1

Status
Not open for further replies.

EastIndian

Technical User
Mar 21, 2002
18
0
0
US
I am trying to ensure that a radio option is selected before the form is processed. It looks like, from the error message that I am getting, that my validation is not working. Included is the portion of my page containing the validation for the radio option. The error message I am receiving when I submit the form without selecting one of the radio options pertains to the radio field in the processing page, saying it does not exist, which makes sence since nothing has been checked. Anyone have any hints form me?

<!--- Get the options for the question... --->
<CFQUERY NAME=&quot;getOptions&quot; DATASOURCE=&quot;#S_datasource#&quot;>
SELECT optionID, questionOoption
FROM options
WHERE surveyID = #listOnesValue#
</CFQUERY>
<CFSET survey = listOnesValue>
</CFIF>
<HTML>
<HEAD>
<TITLE>Validation Testing</TITLE>
<!---
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>

// function to ensure radio is checked.
function CheckValues()
{
// loop through each record.
for ( var i=1; i<=<CFOUTPUT>#GetOptions.RecordCount#</CFOUTPUT>; i++){

if(document.survey.answer.type == &quot;radio&quot;)
{
document.survey.answeri.checked=false;
alert(&quot;You have not selected an answer.&quot;);
return false;
break;
}
}
}
</SCRIPT>
--->
</HEAD>

<form name=&quot;survey&quot; action=&quot;<cfoutput>#surveyResults#</cfoutput>&quot; method=&quot;Post&quot; onSubmit=&quot;return CheckValues()&quot;>

<table cellspacing=0 cellpadding=0 border=0>
<tr><td>

<table width=&quot;<cfoutput>#S_width#</cfoutput>&quot; border=<cfoutput>&quot;#S_border#&quot; </cfoutput>
cellspacing=0 cellpadding=5>
<tr>
<td bgcolor=&quot;<cfoutput>###S_headerColor#</cfoutput>&quot;>
<cfoutput><font face=&quot;#S_headerFont#&quot; size=&quot;-1&quot; color=&quot;#S_headerFontColor#&quot;></cfoutput>
<cfoutput query=&quot;getQuestion&quot;>
<strong>#question#</strong><br>
</cfoutput>
</font>
</td>
</tr>
<tr>
<td bgcolor=&quot;<cfoutput>###S_bodyColor#</cfoutput>&quot;>
<cfoutput><font face=&quot;#S_bodyFont#&quot; size=&quot;-1&quot; color=&quot;#S_bodyFontColor#&quot;></cfoutput>
<CFSET myCount = &quot;0&quot;>
<cfoutput query=&quot;getOptions&quot;>
<input type=&quot;radio&quot; value=&quot;#optionID#&quot; name=&quot;radio&quot;>#questionOption#<br>
<CFSET myCount = myCount + 1>
</cfoutput>
<cfoutput>
<input type=&quot;hidden&quot; name=&quot;survey&quot; value=&quot;#survey#&quot;>
</cfoutput>
<input type=&quot;submit&quot; value=&quot;Submit&quot;></form>
</font>
</td></tr></table>
 
You have to make sure one of the radio buttons has a default CHECKED attribute. If you don't want a default value, you can check it with JavaScript. A good script you might want to take a look at (and the site itself) is:


and on your processing page, you can use cfparam to create a default:

<cfparam name=&quot;FORM.radio&quot; default=&quot;mydefaultvalue&quot;>

Note that you should REALLY validate server side ALL THE TIME, no matter if you are validating client-side. Your apps will be more open to hack attempts if you do no server-side validation, since the user can send any data to your server that they want; e.g. user copies your form and submits it from their computer after they've modified it.

-Tek
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top