Solved

Server-side validation with Coldfusion

Posted on 2007-12-03
7
263 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

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…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

808 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