ColdFusion Tutorial

Validate a sublimated form

Sometimes we need to make sure that the required field in a form contains data.

Example: when saving to a database.

The file add_name.cfm let the user enter the necessary data and submit the information to save_name.cfm.

The file save_name.cfm receives the submitted data and validate if the required field(s) contains data.

This is done by using two variables:
<cfset valid = True> (Boolean variable)
<cfset errormsg = ""> (String variable)

We use <cfif> statement to check if the submitted form is valid.

<cfif valid>
     Insert data into the database.
<cfelse>
     Give error message and give information about field(s) that is not correct.

     Show the user the 'Correct' button, so the user can go back and correct the necessary field(s).

     <input name="Submit" type="submit" onclick="history.back(-1)" value="Correct" />
</cfiff>

add_name.cfm

<form action="save_name.cfm" method="post">
  <p>First Name:
    <input name="First_Name" type="text" size="30" maxlength= "30" />
  </p>
  <p>Last Name:
   <input name="Last_Name" type="text" size="30" maxlength="30" />
  </p>
  <p>Country:
    <input name="Country" type="text" size="30" maxlength="30" />
  </p>
  <p>City:
    <input name="City" type="text" size="30" maxlength="30" />
  </p>
  <p>Zip:
    <input name="Zip" type="text" size="30" maxlength="30" />
  </p>
  <div align="center">
    <input name="" type="submit" />
  </div>
</form>


save_name.cfm

<!--- Start Checking the form --->
<cfset valid = True>
<cfset errormsg = "">

<!--- Check if First Name contains a value. --->  
<cfif Len(Form.First_name) IS 0>
      <cfset valid = False>
      <cfset errormsg = errormsg & "<li>First Name is required.</li><br>">
</cfif>
<!--- Check if Last Name contains a value. --->  
<cfif Len(Form.Last_name) IS 0>
      <cfset valid = False>
      <cfset errormsg = errormsg & "<li>Last Name is required.</li><br>">
</cfif>
<!--- Check if Country contains a value. --->  
<cfif Len(Form.Country) IS 0>
      <cfset valid = False>
      <cfset errormsg = errormsg & "<li>Country is required.</li><br>">
</cfif>
<!--- Check if City contains a value. --->  
<cfif Len(Form.City) IS 0>
      <cfset valid = False>
      <cfset errormsg = errormsg & "<li>City is required.</li><br>">
</cfif>
<!--- Check if Zip contains a value. --->  
<cfif Len(Form.Zip) IS 0>
      <cfset valid = False>
      <cfset errormsg = errormsg & "<li>Zip is required.</li><br>">
</cfif>

<cfif valid>
 <!--- Output submitted data.. --->
     First name: <cfoutput>#Form.First_name#</cfoutput><br />
     Last name: <cfoutput>#Form.Last_name#</cfoutput><br />
     Country: <cfoutput>#Form.Country#</cfoutput><br />
     City: <cfoutput>#Form.City#</cfoutput><br />
     Zip: <cfoutput>#Form.Zip#</cfoutput><br />
<cfelse>
     The form you submitted has the following incomplete or invalid entries:<br /><br />
     <cfoutput>#errormsg#</cfoutput><br />
     Please use the 'Correct' button to return to the previous page and correct the error(s).<br /><br />
     <input name="Submit" type="submit" onclick="history.back(-1)" value="Correct" />
</cfif>

ColdFusion Tutorial test

Validate a sublimated form

No test is available or implemented for this ColdFusion Tutorial.

No User Comments.

No User Comments, be the first one to write your comments?

Add your comments

Your Name*:
Your Email:
Site URL:
Site Name:
Comemt Title*:
Your Comment*:
Key Phrase*:
i5M
Your IP Address: 38.103.63.62
 

ScandicWeb is own by Scandic Systems LTD [UK] Company No. 5984000. All other trademarks and copyrights are the property of their respective holders.

[Home [Site Map [Privacy Statement [Site Feedback] [About ScandicWeb]


[ColdFusion [Flash [Search Engine Optimization [Graphics [Cascading Style Sheets (CSS) [JavaScript (JS) [Software]

Partner sites: ScandicSoft


Server time: 19. November 2008 Wednesday