Solved

Server-side validation with Coldfusion

Posted on 2007-12-03
7
273 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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 500 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

717 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