Solved

Server-side validation with Coldfusion

Posted on 2007-12-03
7
253 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_
Comment Utility
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_
Comment Utility
>  <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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 52

Expert Comment

by:_agx_
Comment Utility
> <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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now