Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Server-side validation with Coldfusion

Posted on 2007-12-03
7
Medium Priority
?
281 Views
Last Modified: 2013-12-24
I'm using Coldfusion to validate a form I created and everything seems to be working fine except the validation on the dropdown boxes (form.state and form.bus_type) - can anyone tell me why it's not working?  Here's my code:

<cfset ErrorFlag = false>
<cfset ErrorMsg = "">
<cfif IsDefined("Form.TestSubmit")>
   <cfif Len(Trim(Form.fname)) EQ 0>
       <cfset ErrorMsg = ListAppend(ErrorMsg, "Please enter your first Name")>
   </cfif>
   <cfif Len(Trim(Form.lname)) EQ 0>
       <cfset ErrorMsg = ListAppend(ErrorMsg, "Please enter your last Name")>
   </cfif>
<cfif Len(Trim(Form.addr1)) EQ 0>
       <cfset ErrorMsg = ListAppend(ErrorMsg, "Please enter your address")>
   </cfif>
<cfif Len(Trim(Form.city)) EQ 0>
       <cfset ErrorMsg = ListAppend(ErrorMsg, "Please enter your city")>
   </cfif>
<cfif Len(Trim(Form.state)) EQ 0>
       <cfset ErrorMsg = ListAppend(ErrorMsg, "Please enter your state")>
   </cfif>
<cfif Len(Trim(Form.bus_type)) EQ 0>
       <cfset ErrorMsg = ListAppend(ErrorMsg, "Please enter your business type")>
   </cfif>

<cfset ErrorFlag = ErrorMsg NEQ "">
   <cfif NOT ErrorFlag>
<cflocation url="compliance_survey.cfm">
   </cfif>
</cfif>

<form action='thispage.cfm' method='post' name='theForm'>
<cfoutput>
<cfif ErrorFlag>
<p class='contact'><font color='##FF0000'>Please correct the following errors:</font></p>
   <ul class='cferrorlist'>
    <cfloop index="error" list="#ErrorMsg#">
         <li>#error#</li>
     </cfloop>
    </ul>
</cfif>
<table class='survey'>
<tr>
<td class='text'><span class='redtext'>* </span><label for='q1'>First Name: </label></td>
<td><input type='text' name='fname' value='<cfif ErrorFlag>#Form.fname#</cfif>' id='q1' /></td>
</tr>
<tr>
<td class='text'><span class='redtext'>* </span><label for='q2'>Last Name: </label></td>
<td><input type='text' name='lname' value='<cfif ErrorFlag>#Form.lname#</cfif>' id='q2' /></td>
</tr>
<tr>
<td class='text'><span class='redtext'>* </span><label for='q7'>Address: </label></td>
<td><input type='text' name='addr1' value='<cfif ErrorFlag>#Form.addr1#</cfif>' id='q7' /></td>
</tr>
<tr>
<td class='text'><span class='redtext'>* </span><label for='q9'>City: </label></td>
<td><input type='text' name='city' value='<cfif ErrorFlag>#Form.city#</cfif>' id='q9' /></td>
</tr>
<tr>
<td class='text'><span class='redtext'>* </span><label for='q10'>State:</label></td>
<td><select name='state' class='fieldtext' id='q10'>
<option value='<cfif ErrorFlag>#Form.state#</cfif>' SELECTED>Please select a state</option>
<optgroup label='intl'>
<option label='other than US or Canada' value='other'>other than US or Canada</option>
</optgroup>
<optgroup label='Canada'>
<option label='Alberta' value='AB'>Alberta</option>
<option label='British Columbia' value='BC'>British Columbia</option>
<option label='Manitoba' value='MB'>Manitoba</option>
<option label='New Brunswick' value='NB'>New Brunswick</option>
<option label='Newfoundland' value='NL'>Newfoundland</option>
<option label='Northwest Territories' value='NT'>Northwest Territories</option>
<option label='Nova Scotia' value='NS'>Nova Scotia</option>
<option label='Nunavut' value='NU'>Nunavut</option>
<option label='Ontario' value='ON'>Ontario</option>
<option label='Prince Edward Island' value='PE'>Prince Edward Island</option>
<option label='Quebec' value='QC'>Quebec</option>
<option label='Saskatchewan' value='SK'>Saskatchewan</option>
<option label='Yukon' value='YT'>Yukon</option>
</optgroup>
<optgroup label='United States'>
<option label='Alabama' value='AL'>Alabama</option>
<option label='Alaska' value='AK'>Alaska</option>
<option label='Arizona' value='AZ'>Arizona</option>
<option label='Arkansas' value='AR'>Arkansas</option>
<option label='California' value='CA'>California</option>
<option label='Colorado' value='CO'>Colorado</option>
<option label='Connecticut' value='CT'>Connecticut</option>
<option label='Delaware' value='DE'>Delaware</option>
<option label='Florida' value='FL'>Florida</option>
<option label='Georgia' value='GA'>Georgia</option>
<option label='Hawaii' value='HI'>Hawaii</option>
<option label='Idaho' value='ID'>Idaho</option>
<option label='Illinois' value='IL'>Illinois</option>
<option label='Indiana' value='IN'>Indiana</option>
<option label='Iowa' value='IA'>Iowa</option>
<option label='Kansas' value='KS'>Kansas</option>
<option label='Kentucky' value='KY'>Kentucky</option>
<option label='Louisiana' value='LS'>Louisiana</option>
<option label='Maine' value='ME'>Maine</option>
<option label='Maryland' value='MD'>Maryland</option>
<option label='Massachusetts' value='MA'>Massachusetts</option>
<option label='Michigan' value='MI'>Michigan</option>
<option label='Minnesota' value='MN'>Minnesota</option>
<option label='Mississippi' value='MS'>Mississippi</option>
<option label='Missouri' value='MO'>Missouri</option>
<option label='Montana' value='MT'>Montana</option>
<option label='Nebraska' value='NE'>Nebraska</option>
<option label='Nevada' value='NV'>Nevada</option>
<option label='New Hampshire' value='NH'>New Hampshire</option>
<option label='New Jersey' value='NJ'>New Jersey</option>
<option label='New Mexico' value='NM'>New Mexico</option>
<option label='New York' value='NY'>New York</option>
<option label='North Carolina' value='NC'>North Carolina</option>
<option label='North Dakota' value='ND'>North Dakota</option>
<option label='Ohio' value='OH'>Ohio</option>
<option label='Oklahoma' value='OK'>Oklahoma</option>
<option label='Oregon' value='OR'>Oregon</option>
<option label='Pennsylvania' value='PA'>Pennsylvania</option>
<option label='Puerto Rico' value='PR'>Puerto Rico</option>
<option label='Rhode Island' value='RI'>Rhode Island</option>
<option label='South Carolina' value='SC'>South Carolina</option>
<option label='South Dakota' value='SD'>South Dakota</option>
<option label='Tennessee' value='TN'>Tennessee</option>
<option label='Texas' value='TX'>Texas</option>
<option label='Utah' value='UT'>Utah</option>
<option label='Vermont' value='VT'>Vermont</option>
<option label='Virgin Islands' value='VI'>Virgin Islands</option>
<option label='Virginia' value='VA'>Virginia</option>
<option label='Washington' value='WA'>Washington</option>
<option label='West Virginia' value='WV'>West Virginia</option>
<option label='Wisconsin' value='WI'>Wisconsin</option>
<option label='Wyoming' value='WY'>Wyoming</option>
</optgroup>
</select></td>
</tr>
<tr>
<td class='text'><span class='redtext'>* </span><label for='q13'>What is your business type?</label></td>
<td><select name='bus_type' value='<cfif ErrorFlag>#Form.bus_type#</cfif>' class='fieldtext'
id='q13'>
                        <option value='0'>Please Select One</option>
                        <option value='type1'>business type 1</option>
                        <option value='type2'>business type 2</option>
                        <option value='type3'>business type 3</option>
         </select></td>
</tr>
</table>

<input type='Submit' name='TestSubmit' VALUE='Continue' class='surveysubmit2' />
</cfoutput>
</form>
0
Comment
Question by:immixGroup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 20400656
Check your default values.  

Also, the first option for bus_type is "0" .  That means this statement
<cfif Len(Trim(Form.bus_type)) EQ 0>

would fail because the the length of "0" is not EQ 0.

0
 
LVL 52

Expert Comment

by:_agx_
ID: 20400666
>  <select name='bus_type'  ....>
>  <option value='0'>Please Select One</option>
> ...

ie.  The default list option value is "0"


0
 
LVL 18

Expert Comment

by:Plucka
ID: 20400734
You need to change these two lines

<option value='0'>Please Select One</option>
to
<option value=''>Please Select One</option>

and

<option value='<cfif ErrorFlag>#Form.state#</cfif>' SELECTED>Please select a state</option>
to
<option value='' SELECTED>Please select a state</option>
0
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

 
LVL 52

Expert Comment

by:_agx_
ID: 20400824
> <option value='' SELECTED>Please select a state</option>

Only if you want the previous list selection to be cleared if the form reloads.
0
 

Author Comment

by:immixGroup
ID: 20403334
Ok, here's what I have now.  It validates now but the selection does not save like the other fields do if I reload the form - how can I fix that?  Here's the snippet of code:

<tr>
<td class='text'><span class='redtext'>* </span><label for='q13'>What is your business type?</label></td>
<td>
<select name='bus_type' value='<cfif ErrorFlag>#Form.bus_type#</cfif>' class='fieldtext' id='q13'>
<option value=''>Please Select One</option>
<option value='bustype1'>Type 1</option>
<option value='bustype2'>Type 2</option>
<option value='bustype3'>Type 3</option>
</select>
</td>
</tr>
0
 

Author Comment

by:immixGroup
ID: 20404744
Sorry, here is the CF validation part - forgot to include this in my last note; I haven't changed this part though --

<cfif Len(Trim(Form.bus_type)) EQ 0>
       <cfset ErrorMsg = ListAppend(ErrorMsg, "Please enter your business type")>
   </cfif>
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 2000 total points
ID: 20404845
Use a cfif to test the value of the submitted bus_type.  If they're equal mark the option as selected


<select name='bus_type' class='fieldtext' id='q13'>
<option value='' <cfif form.bus_type is "">selected</cfif>>Please Select One</option>
<option value='bustype1' <cfif form.bus_type is "bustype1">selected</cfif>>Type 1</option>
<option value='bustype2' <cfif form.bus_type is "bustype2">selected</cfif>>Type 2</option>
<option value='bustype3' <cfif form.bus_type is "bustype3">selected</cfif>>Type 3</option>
</select>
0

Featured Post

Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question