User Guide

Table Of Contents
682 Chapter 28: Validating Data
</body>
</html>
2.
Save the page as validjs.cfm.
3.
View the validjs.cfm page in your browser.
Handling failed validation
The
onError attribute lets you specify a JavaScript function to execute if an onValidate, onBlur
or onSubmit validation fails. For example, if you use the
onValidate attribute to specify a
JavaScript function to handle input validation, you can also use the
onError attribute to specify a
JavaScript function to handle a failed validation (that is, when
onValidate returns a False value).
If you use the
onValidate attribute, you can also use the onError attribute to specify a
JavaScript function that handles the validation errors. The following
cfform tags support the
onerror attribute:
cfgrid
cfinput
cfselect
cfslider
cftextinput
cftree
ColdFusion passes the following JavaScript objects to the function in the
onerror attribute:
The JavaScript form object
The name attribute of the form element
The value that failed validation
The error message text specified by the CFML tags message attribute
The following example shows a form that uses an
onError attribute to tell ColdFusion to call a
showErrorMessage JavaScript function that uses the
alert method to display an error message.
The function assembles the message from the invalid value and the contents of the
cfinput tag’s
message attribute.
<!--- 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. --->