Validating User Input
Coldfusion has some pretty useful internal functions that few people bother using. Most of the people who don't use them live outside of the USA because, unfortunatly, some of the more useful validation is for the US only (ZipCode, SSN, Telephone Numbers etc). However, if you do live in the USA this function is quite useful!
The isValid Function
The function takes up to four parameters. The simplest form of the function takes two parameters, type, and value.
For example:
<cfset validPhoneNumber = isValid("telephone","(202) 248-1254")/>
<cfif validPhoneNumber>
the phone number is valid
<cfelse>
the phone number is not valid
</cfif>
The result of the above would be "the phone number is valid"
Other Input Types
The isValid function can test the validity of a lot of different types of values. The types that would be most useful for testing input from users are as follows:- creditcard: All major creditcards conform to a specific algorithm, using this type will allow you to pre-check the validity of a credit card number.
- email: This is a simple check to see if the email address conforms to the standard pattern, it naturally can't check if the email address exists or not.
- eurodate: Checks the value to see if the date entered is correct for european date formats
- zipcode: Checks that the value is a valid US zip code, would be nice to be able to check any country though wouldnt it?
- URL: Checks that the value conforms to the URL pattern.
- USdate: Checks the date for the US date format mm/dd/yy or with only single digit months and days as well as 2 or 4 digit years.
- float: Allows you to check that the number includes a decimal or not.
- ssn or social_security_number: As expected checks that the number conforms to the US social security number specification.
Conclusion
Before you try to come up with your own algorithm and or regular expression to perform input validation be sure to check the documentation, if you don't find what you are looking for there try searching on CFLIB for a user defined function that someone else has kindly uploaded.