User Guide

Table Of Contents
684 Chapter 28: Validating Data
<cfform action="#CGI.SCRIPT_NAME#">
User ID:<cfinput type="Text" name="UserID"><br>
Phone: <cfinput type="Text" name="phoneNo"><br>
E-mail: <cfinput type="Text" name="emailAddr"><br>
<cfinput type="submit" name="saveSubmit" value="Save Data"><br>
</cfform>
Examples: cfparam tag validation
The following two examples use
cfparam tags to do the same tests as in the “Example: IsValid
function validation” on page 683. They check whether a user has submitted a numeric ID and a
valid e-mail address and phone number. If any of the submitted values does not meet the
validation test, ColdFusion throws an expression exception.
In the first example, the error is handled by the exprerr.cfm page specified in the
cferror tag. In
this case, if the user made multiple errors, ColdFusion lists only one.
In the second example, each invalid field is handled in a separate try/catch block. In this case, the
user gets information about each error.
Using an error-handling page
The self-posting form and action page looks as follows:
<!--- Action part of the page. --->
<!--- If an expression exception occurs, run the expresser.cfm page. --->
<cferror type="EXCEPTION" exception="expression" template="expresserr.cfm">
<!--- Make sure the form was submitted. --->
<cfif isDefined("form.saveSubmit")>
<!--- Use cfparam tags to check the form field data types. --->
<cfparam name="form.emailAddr" type="email">
<cfparam name="form.UserID" type="integer">
<cfparam name="form.phoneNo" type="telephone">
<!--- Application code to update the database goes here. --->
<cfoutput>
<h3>The e-mail address and phone number for user #Form.UserID#
have been added</h3>
</cfoutput>
</cfif>
<!--- The form. --->
<cfform action="#CGI.SCRIPT_NAME#">
User ID:<cfinput type="Text" name="UserID"><br>
Phone: <cfinput type="Text" name="phoneNo"><br>
E-mail: <cfinput type="Text" name="emailAddr"><br>
<cfinput type="submit" name="saveSubmit" value="Save Data"><br>
</cfform>
The expresserr.cfm page looks as follows:
<cfoutput>
You entered invalid data.<br>
Please click the browser Back button and try again<br>
#cferror.RootCause.detailMessage#
</cfoutput>