User Guide
140 Chapter 9 Building Dynamic Forms
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
RERplacenocase and in ColdFusion Studio. For information on regular expressions
used in ColdFusion functions, see “Using Regular Expressions in Functions” on page
259.
Special characters
Because special characters are the operators in regular expressions, in order to
represent a special character as an ordinary one, you need to precede it with a
backslash. For example, use a double backslash (\\) to represent a backslash.
Single-character regular expressions
The following rules govern regular expressions that match a single character:
• Special characters are: + * ? . [ ^ $ ( ) { | \
• Any character that is not a special character or escaped by being preceded by the
backslash (\) matches itself.
• A backslash (\) followed by any special character matches the literal character
itself, that is, the backslash escapes the special character.
• A period (.) matches any character except newline.
• A set of characters enclosed in brackets ([]) is a one-character regular expression
that matches any of the characters in that set. For example, "[akm]" matches an
"a", "k", or "m". Note that if you want to include ] (closing square bracket) in
square brackets it must be the first character. Otherwise, it does not work even if
you use \].
• A dash can indicate a range of characters. For example, "[a-z]" matches any
lowercase letter.