Input validation with cfform controls

The cfinput and cftextinput tags include the validate attributes, which lets you specify a valid data entry type for the control. You can validate user entries on the following data types:
Data type
Description
Date
Verifies US date entry in the form mm/dd/yyyy (where the year can have one through four digits).
Eurodate
Verifies valid European date entry in the form dd/mm/yyyy (where the year can have one through four digits).
Time
Verifies a time entry in the form hh:mm:ss.
Float
Verifies a floating point entry.
Integer
Verifies an integer entry.
Telephone
Verifies a telephone entry. You must enter telephone data as ###-###-####. You can replace the hyphen separator (-) with a blank. The area code and exchange must begin with a digit between 1 and 9.
Zipcode
(U.S. formats only) Number can be a five-digit or nine-digit zip in the form #####-####. You can replace the hyphen separator (-) with a blank.
Creditcard
Blanks and dashes are stripped and the number is verified using the mod10 algorithm.
Social_security_number
You must enter the number as ###-##-####. You can replace the hyphen separator (-) with a blank.
Regular_expression
Matches the input against a JavaScript regular expression pattern. You must use the pattern attribute to specify the regular expression. Any entry containing characters that matches the pattern is valid.

When you specify an input type in the validate attribute, ColdFusion tests for the specified input type when you submit the form, and submits form data only on a successful match. A successful form submission returns the value True and returns the value False if validation fails.

Validating with regular expressions

You can use regular expressions to match and validate the text that users enter in cfinput and cftextinput tags. Ordinary characters are combined with special characters to define the match pattern. The validation succeeds only if the user input matches the pattern.

Regular expressions allow you to check input text for a wide variety of conditions. For example, if a date field must only contain dates between 1950 and 2050, you can create a regular expression that matches only numbers in that range. You can concatenate simple regular expressions into complex search criteria to validate against complex patterns, such as any of several words with different endings.

You can use ColdFusion variables and functions in regular expressions. The ColdFusion Server evaluates the variables and functions before the regular expression is evaluated. For example, you can validate against a value that you generate dynamically from other input data or database values.

Note:   The rules listed in this section are for JavaScript regular expressions, and apply to the regular expressions used in cfinput and cftextinput tags only. These rules differ from those used by the ColdFusion functions REFind, REReplace, REFindNoCase, and REReplaceNoCase. For information on regular expressions used in ColdFusion functions, see Chapter 7, "Using Regular Expressions in Functions".

Special characters

Because special characters are the operators in regular expressions, in order to represent a special character as an ordinary one, you must precede it with a backslash. For example, use double backslash characters (\\) to represent a backslash character.

Single-character regular expressions

The following rules govern regular expressions that match a single character:

Multicharacter regular expressions

Use the following rules to build a multicharacter regular expression:

Backreferences

Backreferencing lets you match text in previously matched sets of parentheses. A slash followed by a digit n (\n) refers to the nth parenthesized subexpression.

One example of how you can use backreferencing is searching for doubled words; for example, to find instances of 'the the' or 'is is' in text. The following example shows the syntax you use for backreferencing in regular expressions:

(\b[A-Za-z]+)[ ]+\1

This code matches text that contains a word (specified by the \b word boundary special character and the [A-Za-z]+) followed by one or more spaces [ ]+, followed by the first matched subexpression in parentheses. For example, it would match "is is, or "This is is", but not "This is".

Exact and partial matches

Entered data is normally valid if any of it matches the regular expression pattern. Often you might ensure that the entire entry matches the pattern. If so, you must "anchor" it to the beginning and end of the field as follows:

Expression examples

The following examples show some regular expressions and describe what they match:
Expression
Description
[\?&]value=
Any string containing a URL parameter value.
^[A-Z]:(\\[A-Z0-9_]+)+$
An uppercase DOS/Windows directory path that is not the root of a drive and has only letters, numbers, and underscores in its text.
^(\+|-)?[1-9][0-9]*$
An integer that does not begin with a zero and has an optional sign.
^(\+|-)?[1-9][0-9]*(\.[0-9]*)?$
A real number.
^(\+|-)?[1-9]\.[0-9]*E(\+|-)?[0-9]+$
A real number in engineering notation.
a{2,4}
A string containing two to four occurrences of 'a': aa, aaa, aaaa; for example aardvark, but not automatic.
(ba){2,}
A string containing least two 'ba' pairs; for example Ali baba, but not Ali Baba.

Resources

An excellent reference on regular expressions is Mastering Regular Expressions by Jeffrey E.F. Friedl, published by O'Reilly & Associates, Inc.

Comments