Programming instructions

Developing code to validate data and enforce business rules 107
the network and the server. If the data is validated on the client, then only valid data is
posted to the server and traffic is reduced.
Validating data on the client using ColdFusion form tags
An alternative approach to server-side editing is to use client-side scripting. Client-side
scripting lets you validate the form data entered on the client prior to posting it to the
server. CFML provides alternative versions of standard HTML form tags that provide
advantages of client-side data validation.These data input tags include
cfinput text,
cfinput radio, cfinput checkbox, cfselect, and others.
Among the improvements over standard HTML tags, ColdFusion form tags offer the
following attributes:
Examples of using the improved ColdFusion form tags
To use the improved form tags, you must replace the HTML form tag with the
cfform
tag. The following code snippets show the use of the improved ColdFusion form tags.
The first code snippet shows how the duration field is validated on the server. The second
code snippet shows how ColdFusion form tags simplify field validation on the client.
Server-side validation approach (no ColdFusion form tag)
The following code is on the client (tripedit.cfm page):
<input size=3 name=duration>
Code on the server (tripeditaction.cfm page):
<!--- Duration is Required and must be Numeric --->
<cfif Form.numberPeople EQ "" or IsNumeric(Form.numberPeople) EQ False>
<cfset IsOk = "No">
<cfoutput>The number of people must be a number and cannot be blank.
</cfoutput>
</cfif>
Attribute Description
validate The data type that the field tag validates against. Values include: integer,
date, time, telephone, zipcode.
message The error message displayed if validation fails.
range The range of permissible values for this tag.
required An indicator of whether data is required for the corresponding tag.
Code Explanation
<cfif Form.numberPeople EQ "" or
IsNumeric(Form.numberPeople) EQ False>
The cfif tag evaluates the value of the form
variable numberPeople to determine if the user
entered a value. The IsNumeric function checks
whether the value entered on the form was a
numeric value.