The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 19, 2019, 05:40:33 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Form Validation CFC  (Read 2265 times)
KaptainKrayola
Keeper of Pie
Global Moderator
Lifer
*****
Offline Offline

Posts: 994



View Profile WWW
« on: November 12, 2008, 08:52:22 AM »

This fairly simple yet handy CFC will validate forms (or whatever input really) for you and return a structure with the fields that didn't pass validation.

It will validate:
- null inputs for required fields
- numeric input
- email addresses
- phone numbers
- zip codes
- credit card numbers

To use you pass in the entire form and tell it what the field names are for the different types of fields.  You can also specify and ignore list of field names for fields that are optional on your form.

Enjoy and feel free to modify it to suit your needs.

Code:
<cfcomponent>
<cffunction name="validateForm" access="public" returntype="struct">
<cfargument name="formInfo" type="struct" required="yes">
<cfargument name="ignore" type="string" required="no" default="0">
<cfargument name="emailfield" type="string" required="no" default="0">
<cfargument name="numberfield" type="string" required="no" default="0">
<cfargument name="zipcodefield" type="string" required="no" default="0">
<cfargument name="phoneNumberField" type="string" required="no" default="0">
<cfargument name="creditCardField" type="string" required="no" default="0">

<cfset var stError = structNew()>
<!--- <cfset stError.message = structNew()> --->

<cfif NOT structIsEmpty(formInfo)>

<!--- validate requierd fields ---->
<cfloop list="#arguments.forminfo.fieldnames#" index="f">
<cfif NOT listFindnoCase(arguments.ignore,f)>
<cfset curField = arguments.formInfo[f]>
<cfif curField EQ "">
<cfset stError[f] = 1>
</cfif>
<!--- validaet email fields --->
<cfif listFindnoCase(arguments.emailfield,f) AND len(curField)>
<cfif NOT listFindnoCase(arguments.ignore,f) AND NOT validateEmail(curField)>
<cfset stError[f] = 1>
</cfif>
</cfif>
<!--- validate numeric fields ---->
<cfif listFindnoCase(arguments.numberfield,f) AND len(curField)>
<cfif NOT listFindnoCase(arguments.ignore,f) AND NOT isNumeric(curField)>
<cfset stError[f] = 1>
</cfif>
</cfif>
<!--- validate zipcode fields ---->
<cfif listFindnoCase(arguments.zipcodefield,f) AND len(curField)>
<cfif NOT listFindnoCase(arguments.ignore,f) AND NOT validateZip(curField)>
<cfset stError[f] = 1>
</cfif>
</cfif>
<!--- validate telephone fields ---->
<cfif listFindnoCase(arguments.phoneNumberField,f) AND len(curField)>
<cfif NOT listFindnoCase(arguments.ignore,f) AND NOT validatePhone(curField)>
<cfset stError[f] = 1>
</cfif>
</cfif>
<!--- validate credit card number fields ---->
<cfif listFindnoCase(arguments.creditCardField,f) AND len(curField)>
<cfif NOT listFindnoCase(arguments.ignore,f) AND NOT validateCreditCard(curField)>
<cfset stError[f] = 1>
</cfif>
</cfif>

</cfif>
</cfloop>
<cfelse>
<cfset stError.emptyForm = 1>
</cfif>
<cfreturn stError>
</cffunction>

<cffunction name="validateEmail" access="public" returntype="boolean">
<cfargument name="email" type="string" required="yes">
<cfreturn isValid("email",arguments.email)>
</cffunction>
<cffunction name="validatePhone" access="public" returntype="boolean">
<cfargument name="phone" type="string" required="yes">
<cfreturn isValid("telephone",arguments.phone)>
</cffunction>
<cffunction name="validateZip" access="public" returntype="boolean">
<cfargument name="zip" type="string" required="yes">
<cfreturn isValid("zipcode",arguments.zip)>
</cffunction>
<cffunction name="validateCreditCard" access="public" returntype="boolean">
<cfargument name="creditCard" type="string" required="yes">
<cfreturn isValid("creditcard",arguments.creditCard)>
</cffunction>
</cfcomponent>

« Last Edit: November 12, 2008, 08:57:46 AM by KaptainKrayola » Logged

We can't stop here, this is bat country.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: November 12, 2008, 09:20:19 AM »

Welcome back, Kaptain - we missed you Wink
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
KaptainKrayola
Keeper of Pie
Global Moderator
Lifer
*****
Offline Offline

Posts: 994



View Profile WWW
« Reply #2 on: November 12, 2008, 10:29:52 AM »

I have some more goodies to post today too, and thanks it's good to be back for today  ROFLMAO
Logged

We can't stop here, this is bat country.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!