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:
cfparam
tag with the type
attribute to validate any variable.
input
tag with a hidden
attribute to validate the contents of a form input field. For information on this technique, see "Validating form field data types," in Chapter 26.cfform
controls that have validation attributes. For information on using cfform
tags, see Chapter 27, "Building Dynamic Forms".cfqueryparam
tag in a SQL WHERE clause to validate query parameters. For information on this technique, see "Using cfqueryparam," in Chapter 20.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.
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:
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.