Solved

Server-side validation with Coldfusion

Posted on 2007-12-03
7
266 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

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.

Question has a verified solution.

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

Suggested Solutions

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

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