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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox and radio group validation

Status
Not open for further replies.

richj55

Technical User
Jul 14, 2000
30
US
I have a form that contains one checkbox group and one radio button group. I would like to validate the form to ensure that at least one checkbox is selected and one radio button is selected. "form.competition" is the checkbox group and "form.radiogroup1" is the radio button group. This code gives me an error. Any advice?

<CFIF form.name_REQ is not &quot;&quot; AND
form.company_REQ is not &quot;&quot; AND
form.phone_REQ is not &quot;&quot; AND
form.competition is not &quot;&quot; AND
form.radiogroup1 is not &quot;&quot;>

<cfmail
TO=&quot;email@hotmail.com&quot;
From=&quot;#form.sender#&quot;
Subject=&quot;Online Request For Quote&quot;>
#DateFormat (Now())# #TimeFormat(Now())#

----------Contact Information----------
Name: #form.name_REQ#
Company: #form.company_REQ#
Address: #form.address#
City,State,Zip: #form.citystatezip#
Phone: #form.phone_REQ#
Ext.: #form.phonext#
Fax: #form.fax#
email: #form.sender#

----------Application Information----------
Application: #form.application#
Product Type: #form.product#
Speed: #form.CPMspeed#
First Priority: #form.priorityone#
Second Priority: #form.prioritytwo#
Construction: #form.frametype#
Closure Type: #form.closuremethod#
Pack Configuration: #form.packpattern#
Funding: #form.funding#
Samples: #form.samples#

----------Competition----------
#form.competition#


----------Project Description----------
Description: #form.Description#


----------Carton or Case----------
#form.radiogroup1#


----------Dimensions----------
Length 1: #form.LOne#
Width 1: #form.WOne#
Depth 1: #form.DOne#

Length 2: #form.LTwo#
Width 2: #form.WTwo#
Depth 2: #form.DTwo#

Length 3: #form.LThree#
Width 3: #form.WThree#
Depth 3: #form.DThree#

--------------- end ---------------


</cfmail>
<cfoutput>
<div align=&quot;right&quot;>#DateFormat (Now())# #TimeFormat(Now())# </div><br>
<br>
Thank you for your request #form.name_REQ#. We will process your request and contact you as soon as possible.<BR>
<br>
<div align=&quot;center&quot;><a href=&quot;../home.html&quot;>Return to Homepage</a> </div>
</cfoutput>


<cfelse>

<H3>You are missing a required piece of information for this form to be processed.</H3>
<br>
Please <a href=&quot;javascript:history.go(-1)&quot;>Click here</a> to go back and complete all fields marked with an asterisk(*).
</CFIF>
 
Radio Buttons and checkboxed are different than all the other form fields in that if they are not checked, then the form variable isn't passed at all... not even an empty form variable.

So there are 2 ways to doing this.

1. Create a default variable using <cfparam>

=== START CODE EXAMPLE ===
<cfparam name=&quot;form.competition&quot; default=&quot;&quot;>
<cfparam name=&quot;form.radiogroup1&quot; default=&quot;&quot;>


<CFIF form.name_REQ is not &quot;&quot; AND
form.company_REQ is not &quot;&quot; AND
form.phone_REQ is not &quot;&quot; AND
form.competition is not &quot;&quot; AND
form.radiogroup1 is not &quot;&quot;>

=== END CODE EXAMPLE ===

2. Use the IsDefined(&quot;variable_name&quot;) function:

=== START CODE EXAMPLE ===
<CFIF form.name_REQ is not &quot;&quot; AND
form.company_REQ is not &quot;&quot; AND
form.phone_REQ is not &quot;&quot; AND
IsDefined(&quot;form.competition&quot;) AND
IsDefined(&quot;form.radiogroup1&quot;)>

=== END CODE EXAMPLE === - tleish
 
Thanks tleish. I used your second option and it works great.
 
one more thing that might help speed things up here:
- radio buttons with the same name can be checked only one at the time, so, you can check the default and ensure that at least one checked button is submitted (the radio once checked can not be unchecked):

<input type=&quot;Radio&quot; name=&quot;bName&quot; value=&quot;null&quot; checked>
<input type=&quot;Radio&quot; name=&quot;bName&quot; value=&quot;1&quot;>
<input type=&quot;Radio&quot; name=&quot;bName&quot; value=&quot;2&quot;>

- check boxes on the other side are bit different; if all boxes have the same name, all the values of the checked boses are passed as a list:

if only first and second checbox is checked, the following is passed:

<input type=&quot;Checkbox&quot; name=&quot;cBoxName&quot; value=&quot;0&quot;>
<input type=&quot;Checkbox&quot; name=&quot;cBoxName&quot; value=&quot;1&quot;>
<input type=&quot;Checkbox&quot; name=&quot;cBoxName&quot; value=&quot;2&quot;>

and can be accessed as a list:

<cfoutput>#form.cBoxName#</cfoutput>

the result will be: 0,1

so, regardless of how many checboxes you acctually have, you can check if any of them is checked with one line:

IsDefined(&quot;form.cBoxName&quot;)

and see that all three are checked:

ListLen(form.cBoxName) EQ 3

you can also add a hidden field with the same name; this way the form.cBoxName is always defined but if its walue is the value of the hidden field, you know that no other checkboxes are checked:

<input type=&quot;Hidden&quot; name=&quot;cBoxName&quot; value=&quot;&quot;>
<input type=&quot;Checkbox&quot; name=&quot;cBoxName&quot; value=&quot;0&quot;>
<input type=&quot;Checkbox&quot; name=&quot;cBoxName&quot; value=&quot;1&quot;>
<input type=&quot;Checkbox&quot; name=&quot;cBoxName&quot; value=&quot;2&quot;>

cfif NOT Len(form.cBoxName) - if this is true, no other checboxes are checked
Sylvano
dsylvano@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top