User Guide

Table Of Contents
Handling invalid data 669
Handling invalid data
How you handle invalid data depends on the validation type. This section describes validation
error-handling rules and considerations. For detailed information on error handling in
ColdFusion, including invalid data handling, see Chapter 14, “Handling Errors,” on page 307.
For onBlur, onSubmit, or onServer validation, you can use the cfinput or cftextarea tag’s
message attribute to specify a text-only error message to display. Otherwise, ColdFusion MX
uses a default message that includes the name of the form field that was invalid. (For OnServer
validation, you can customize this message, as described in “Handling form field validation
errors” on page 315.) The following example displays an error message when the user enters an
invalid e-mail address:
E-mail: <cfinput type="text" size="25" name="email"
validate="email" message="You must enter a valid e-mail address.">
For hidden form validation, you can specify a text-only error message in the hidden field’s
value attribute. Otherwise, ColdFusion MX uses a default message that includes the name of
the form field that was invalid. (You can customize this message, as described in “Handling
form field validation errors” on page 315.) The following
cfinput tag, for example, uses a
hidden field validation to display an error message if the user enters an invalid address. (It uses
onServer validation to display a different error message if the user fails to enter a number.)
Telephone: <cfinput type="text" size="20" name="telephone"
validateat="onServer" required="Yes"
message="You must enter a telephone number">
<cfinput type="hidden" name="telephone_cfformtelephone"
value="The number you entered is not in the correct format.<br>Use a
number such as (617) 555-1212, 617-555-1212, or 617-555-1212 x12345">
For HTML and XML format forms (using ColdFusion MX skins), most ColdFusion form tags
have an
onError attribute that lets you specify a Javascript function to run if an onSubmit
error occurs.
For the IsValid function, you write separate code paths to handle valid and invalid data. The
following example shows a simplified case that displays an error message if the user entered an
invalid e-mail address, or a different message if the address is valid:
<cfif IsValid("email", custEmail)>
Thank you for entering a valid address.
<!--- More processing would go here. --->
<cfelse>
You must enter a valid e-mail address.<br>
Click the Back button and try again.
</cfif>