Adobe 38043740 - ColdFusion Standard - Mac Development Manual page 767

Developing applications
Hide thumbs Also See for 38043740 - ColdFusion Standard - Mac:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Requesting and Presenting Information
• The error message text specified by the CFML tag's
The following example shows a form that uses an
JavaScript function that uses the
the invalid value and the contents of the
<!--- The JavaScript function to handle errors.
Puts a message, including the field name and value, in an alert box. --->
<script>
<!--
function showErrorMessage(form, ctrl, value, message) {
alert("The value " + value +" of the " + ctrl + " field " + message);
}
//-->
</script>
<!--- The form.
The cfinput tags use the onError attribute to override the ColdFusion
default error message mechanism. --->
<cfform>
<!--- A minimum quantity is required and must be a number. --->
Minimum Quantity: <cfinput type="Text" name="MinQuantity"
onError="showErrorMessage" validate="numeric" required="Yes"
message="is not a number." ><br>
<!--- A maximum quantity is optional, but must be a number if supplied. --->
Maximum Quantity: <cfinput type="Text" name="MaxQuantity"
onError="showErrorMessage" validate="numeric"
message="is not a number." ><br>
<cfinput type="submit" name="submitit">
</cfform>
Validating data with the IsValid function and the cfparam tag
The
function and
IsValid
cfparam
reside entirely on the ColdFusion server, they can provide a secure mechanism for ensuring that the required
validation steps get performed. Users cannot evade any of the checks by modifying the form data that gets submitted.
These techniques also provide greater flexibility in how you respond to user errors, because you can use full CFML
syntax in your error-handling code.
These two validation techniques operate as follows:
• The
function tests the value of a ColdFusion variable. If the value is valid, it returns True; if the value is
IsValid
invalid, it returns False.
• The
tag with a
cfparam
type
nothing; if the value is invalid, it throws a ColdFusion expression exception.
You can use either technique interchangeably. The technique you choose should depend on your coding style and
programming practices. It can also depend on the specific information that you want to display if an error occurs.
Example: IsValid function validation
The following example checks 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, the page displays an error message.
message
attribute to tell ColdFusion to call a showErrorMessage
onError
method to display an error message. The function assembles the message from
alert
tag's
cfinput
message
tag validate any ColdFusion variable value, not just forms variables. Because they
attribute tests the value of a ColdFusion value for validity. If the value is valid, it does
Last updated 1/20/2012
attribute
attribute.
762

Advertisement

Table of Contents
loading

This manual is also suitable for:

Coldfusion 9

Table of Contents