Important alert: (current site time 7/16/2013 6:44:49 AM EDT)
 

VB icon

All-In-One Form Validator

Email
Submitted on: 6/27/2002 4:47:00 PM
By: dacaplan 
Level: Advanced
User Rating: By 2 Users
Compatibility: Cold Fusion 4.5
Views: 14791
 
      This is a custom tag for validating different types of form fields. You can specify which fields are optional in the "lOptFields" list, or you can specify which fields are required in the "lReqFields" list. You can specify which fields to validate for numeric values in the "lNumFields" list. You can specify which fields to validate for date values in the "lDateFields" list. You can specify which fields to validate for email values in the "lEmailFields" list. You can specify which fields to validate for zip code values (5 digits) in the "lZipFields" list. You can specify which fields to validate for phone number values in the "lPhoneFields" list (10 digits - no mask). The tag returns a variable "errs" which will be 0 if there are no errors. It also returns a comma delimited list of error codes in "lstErrors" which specify the type of errors which occurred. If you do not specify which fields to validate (lOptFields or lReqFields) the whole form will be validated for blank fields. Usage: ...
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
// for :All-In-One Form Validator
//**************************************
© David Caplan 2002
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: All-In-One Form Validator
// Description:
		This is a custom tag for validating different types of form fields. You can specify
		which fields are optional in the "lOptFields" list, or you can specify which fields
		are	required in the "lReqFields" list. You can specify which fields to validate
		for	numeric values in the "lNumFields" list. You can specify which fields to validate
		for	date values in the "lDateFields" list. You can specify which fields to validate
		for	email values in the "lEmailFields" list. You can specify which fields to validate
		for	zip code values (5 digits) in the "lZipFields" list. You can specify which fields 
		to validate for	phone number values in the "lPhoneFields" list (10 digits - no mask).
 		
		The tag returns a variable "errs" which will be 0 if there are no errors. It also 
		returns a comma delimited list of error codes in "lstErrors" which specify the type 
		of errors which occurred.
				
		If you do not specify which fields to validate (lOptFields or lReqFields) the whole 
		form will be validated for blank fields.
Usage:
	
	<cf_FormValidator
	 stForm="#form#"
	 lOptFields="field1,field2,field3..."
	 lReqFields="field1,field2,field3..."
	 lNumFields="field1,field2,..."
	 lDateFields="field1,field2,...">
	 
	 <cfif variables.errs neq 0>
		<form><input type="Button" onClick="history.go(-1);" value="Back"></form>
	<cfelse>
	...
	</cfif>
		
// By: dacaplan
//
// Inputs:Attributes:
		stForm (structure, required attribute) - form structure
		lNumFields (list, *optional attribute) - list of field names that must be numeric
		lOptFields (list, *optional attribute) - list of optional field names
		lReqFields (list, *optional attribute) - list of required field names
		lDateFields (list, *optional attribute) - list of field names that must be dates
		lEmailFields (list, *optional attribute) - list of field names that must be valid addresses
		lZipFields (list, *optional attribute) - list of field names that must be 5 digits long
		lPhoneFields (list, *optional attribute) - list of field names that must be 10 digits long
		
		Note: You cannot define both lReqFields and lOptFields in the tag.
//
// Returns:Returns:
	
		lstErrors = A comma delimited list of all errors by fieldname.
		errs = 0 if there are no errors.
		Error codes (as defined in errs variable):
		1 - required field error 
		2 - number field error
		3 - date field error
		4 - email field error
		5 - zip field error
		6 -	phone field error
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=53&lngWId=9//for details.//**************************************

<!--- Initialize params --->
<cfparam name="attributes.stForm" default="#form#">
<cfset stform="#attributes.stForm#">
<!--- Create the Structures --->	
<cfscript>
	stOptFields = StructNew();
	stReqFields = StructNew();
	stNumFields = StructNew();
	stDateFields = StructNew();	
	stEmailFields = StructNew();
	stZipFields = StructNew();
	stPhoneFields = StructNew();
	variables.errs = 0;
	variables.lstErrors = "";
</cfscript>
<!--- If both input lists are specified, return an error --->
<cfif isDefined("attributes.lOptFields") and isdefined("attributes.lReqFields")> 
	<cfset variables.errs = 6>
	<cfset variables.lstErrors = "Cannot specify 'lOptFields' and 'lReqFields' in frmValidator tag.">
	
<!--- If both input lists are missing, validate all fields --->
<cfelseif Not isDefined("attributes.lOptFields") and Not isDefined("attributes.lReqFields")> 
		<cfloop collection="#stForm#" item="i">
 			<cfif StructFind(stform,i) eq "">
				<cfscript>
					StructInsert(stReqFields, i, "empty");
		</cfscript> 
 			</cfif>	
		</cfloop>
</cfif>
<!--- Validate optional fields --->
<cfif isDefined("attributes.lOptFields")> 
	<!--- Populate first structure with list of optional fields --->
	<cfloop list="#attributes.lOptFields#" index="i">
		<cfscript>
			StructInsert(stOptFields, i, evaluate(i));
		</cfscript>
	</cfloop>
	
	<!--- Populate second structure with empty required fields --->
	<cfloop collection="#stForm#" item="i">
	 <cfif NOT structKeyExists(stOptfields, i)>
	 	<cfif StructFind(stForm,i) eq "">
			<cfscript>
				StructInsert(stReqFields, i, "empty");
	</cfscript>
		</cfif>
	 </cfif>
	</cfloop>
<!--- Validate required fields --->
<cfelseif isDefined("attributes.lReqFields")> 
	<!--- Populate structure with list of empty required fields --->
	<cfloop list="#attributes.lReqFields#" index="i">
		<cfif evaluate(i) eq "">
			<cfscript>
				StructInsert(stReqFields,i,"empty");
			</cfscript>
		</cfif>
	</cfloop>
</cfif>
<cfif isDefined("attributes.lNumFields")> 
	
	<!--- Populate structure with list of non-numeric fields --->
	<cfloop list="#attributes.lNumFields#" index="i">
		<cfif NOT isNumeric(evaluate(i)) and evaluate(i) neq "">
			<cfscript>
				StructInsert(stNumFields,i,evaluate(i));
			</cfscript>
		</cfif>
	</cfloop>
</cfif>
<cfif isDefined("attributes.lDateFields")> 
	<!--- Populate structure with list of date fields --->
	<cfloop list="#attributes.lDateFields#" index="i">
		<cfif NOT isDate(evaluate(i)) and evaluate(i) neq "">
			<cfscript>
				StructInsert(stDateFields,i,evaluate(i));
			</cfscript>
		</cfif>
	</cfloop>
</cfif>
<cfif isDefined("attributes.lEmailFields")> 
	<cfset EmailRegex = "^[[:alnum:]_\.\-]+@([[:alnum:]_\.\-]+\.)+[[:alpha:]]{2,4}$">
	
	<!--- Populate structure with list of badly formed email addresses --->
	<cfloop list="#attributes.lEmailFields#" index="i">
		<cfif NOT REFind(EmailRegex,evaluate(i)) and evaluate(i) neq "">
			<cfscript>
				StructInsert(stEmailFields,i,evaluate(i));
			</cfscript>
		</cfif>
	</cfloop>
</cfif>
<cfif isDefined("attributes.lZipFields")> 
	
	<!--- Populate structure with list of date fields --->
	<cfloop list="#attributes.lZipFields#" index="i">
		<cfif (Len(Trim(evaluate(i))) neq 5 or NOT isNumeric(evaluate(i))) and Trim(evaluate(i)) neq "">
			<cfscript>
				StructInsert(stZipFields,i,evaluate(i));
			</cfscript>
		</cfif>
	</cfloop>
</cfif>
<cfif isDefined("attributes.lPhoneFields")> 
	
	<!--- Populate structure with list of date fields --->
	<cfloop list="#attributes.lPhoneFields#" index="i">
		<cfif (Len(Trim(evaluate(i))) neq 10 or NOT isNumeric(evaluate(i))) and Trim(evaluate(i)) neq "">
			<cfscript>
				StructInsert(stPhoneFields,i,evaluate(i));
			</cfscript>
		</cfif>
	</cfloop>
</cfif>
<!--- Create error messages --->	
<cfif not structIsEmpty(stReqFields)>
	<cfset variables.errs = 1>
			<cfloop collection="#stReqFields#" item="fieldName">
				<cfset variables.lstErrors = ListAppend(lstErrors,lcase(fieldName))>
			</cfloop>
<cfelseif not structIsEmpty(stNumFields)>
		<cfset variables.errs = 2>
			<cfloop collection="#stNumFields#" item="fieldName">
				<cfset variables.lstErrors = ListAppend(lstErrors,lcase(fieldName))>
			</cfloop>
<cfelseif not structIsEmpty(stDateFields)>
		<cfset variables.errs = 3>
			<cfloop collection="#stDateFields#" item="fieldName">
				<cfset variables.lstErrors = ListAppend(lstErrors,lcase(fieldName))>
			</cfloop>
<cfelseif not structIsEmpty(stEmailFields)>
		<cfset variables.errs = 4>
			<cfloop collection="#stEmailFields#" item="fieldName">
				<cfset variables.lstErrors = ListAppend(lstErrors,lcase(fieldName))>
			</cfloop>
			
<cfelseif not structIsEmpty(stZipFields)>
		<cfset variables.errs = 5>
			<cfloop collection="#stZipFields#" item="fieldName">
				<cfset variables.lstErrors = ListAppend(lstErrors,lcase(fieldName))>
			</cfloop>
<cfelseif not structIsEmpty(stPhoneFields)>
		<cfset variables.errs = 6>
			<cfloop collection="#stPhoneFields#" item="fieldName">
				<cfset variables.lstErrors = ListAppend(lstErrors,lcase(fieldName))>
			</cfloop>									
</cfif>
<cfset caller.errs = #variables.errs#>
<cfset caller.lstErrors = #variables.lstErrors#>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

12/20/2005 2:15:33 AMjohnson paul

These tags are very useful,But is there any property to check the file control, object if we provide an invalid file name it should alert immediately.If any one have solution plz do reply, with anticipation..

yours friendly,
Johnson
(If this comment was disrespectful, please report it.)

 
1/18/2006 12:05:19 AMra

sorry I'm wrong using the site...I'mjustgoing to download it.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.