User Guide

Table Of Contents
Validating form data using hidden fields 675
Note: An excellent reference on regular expressions is Mastering Regular Expressions by Jeffrey E.F.
Friedl, published by O'Reilly & Associates, Inc.
Validating form data using hidden fields
ColdFusion lets you specify form field validation on the server by using hidden form fields whose
names consist of the name of the field to validate and the validation type. Hidden field validation
uses the same underlying techniques and algorithms as onServer validation of ColdFusion form
fields.
Hidden field validation has the following features:
You can use it with standard HTML tags. For example, you can validate data in an HTML
input tag. This feature was particularly useful in releases prior to ColdFusion MX 7, because
the
cfinput tag did not support all HTML type attributes.
It is backward-compatible with validation prior to ColdFusion MX 7, when hidden field
validation was the only way to do validation on the server.
Because you use a separate tag for each validation type, if you specify multiple validation rules
for a field, you can specify a different error message for each rule.
You can use hidden field validation with any form field type that submits a data value, not just
input, cfinput, textarea, or cftextarea.
Specifying hidden form field validation
To specify hidden field validation, you do the following:
Create one HTML input element or CFML cfinput tag of type="hidden" for each
validation rule.
Specify the name of the field to validate as the first part of the hidden field name.
Specify the type of validation, starting with an underscore character (_), as the second part of
the hidden field name.
You can specify multiple rules for each form data field. For example, to specify range and
required validation for a field named myValue, create hidden myValue_cfformrange and
myValue_cfformrequired fields.
For most types of validation, specify the error message as the field value attribute.
For range, maximum length, or regular expression validation, specify the rule, such as the
maximum length, in the
value attribute. For these validation types, you cannot specify a
custom error message.
The following example uses hidden fields to require data in a date field and ensure that the field
contains a date. It consists only of HTML tags.
<input type="text" name="StartDate" size="16" maxlength="16"><br>
<input type="hidden" name="StartDate_required"
value="You must enter a start date.">
<input type="hidden" name="StartDate_date"
value="Please enter a valid date as the start date.">