Validating data types

It is often not sufficient that input data merely exists; it must also have the right format. For example, a date field must have data in a date format. A salary field must have data in a numeric or currency format. There are many ways to ensure the validity of data, including the following methods:

Note:   Data validation using the cfparam, cfqueryparam, and form tags is done by the server. Validation using cfform tags is done using JavaScript in the user's browser, before any data is sent to the server.

Using cfparam to validate the data type

The cfparam type attribute lets you validate the type of a parameter. You can specify that the parameter type must be any of the following values:
Type value
Meaning
any
any value
array
any array value
binary
any binary value
boolean
true, false, yes, or no
date
any value in a valid date, time, or date-time format
numeric
any number
query
a query object
string
a text string or single character
struct
a structure
UUID
a Universally Unique Identifier (UUID) formatted as XXXXXXXX-XXXX-XXXX-XXXXXXXXXXXXXXX where X stands for a hexadecimal digit (0-9 or A-F).
variableName
a valid variable name

For example, you can use the following code to validate the variable BirthDate:

<cfparam name="BirthDate" type="date">

If the variable is not in a valid date format, an error occurs and the page stops processing.

Comments