Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple & Fast Variable Validation

Variables

Simple & Fast Variable Validation

by  webmigit  Posted    (Edited  )
One of the things developers hate about forms is the extensive validation involved to stop most hackers in their tracks and of course, to help users keep from making simple mistakes.

This is never easy and even less a pleasant experience.. but I have one method I'll share with you to help.

I create an empty variable called fldTrip.. (Field Trip.. If this is not empty, my validation will trip..).

I set this, I do not use a cfparam because I want this variable empty at the beginning of validation.

Code:
<CFSET fldTrip="">

Next, I want to check all fields that I do not want to be empty.. these are just standard text fields so its ok for them to have a single character value.

Code:
<CFLOOP list="var1,var2,var3" index="i">
  <CFIF Evaluate(I) is "">
   <CFSET fldTrip=ListAppend(fldTrip,"-" & I & "-")>
  </CFIF>
 </CFLOOP>

 <CFIF fldTrip contains "var1">
  <CFSET msgVar1="Message for var1">
 </CFIF>
 <CFIF fldTrip contains "var2">
  <CFSET msgVar2="Message for var2">
 </CFIF>
 <CFIF fldTrip contains "var3">
  <CFSET msgVar3="Message for var3">
 </CFIF>

Now here's the magic part.. to save me a lot of CFIF'ing, I wrote a custom tag to do the dirty work:

--- inerror.cfm ---

Code:
<CFPARAM name="attributes.name" default="">

<CFSET localname=Evaluate("Caller." & Attributes.Name)>

<CFIF caller.fldTrip contains attributes.name>
 <BR><FONT size="-1" color="##FF0000"><CFOUTPUT>#Evaluate("caller.msg#attributes.name#")#</FONT></CFOUTPUT>
</CFIF>

--- /inerror.cfm ---

Now call the tag next to your text boxes.. so you might have...

Code:
<INPUT type="text" name="var1" value=""><CF_inerror name="var1">

And if you've followed all steps, it should work fine..

Tony Hicks
support@alfii.com
Give a visit to http://www.alfii.com/refdoor.cfm
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top