System information
Exercise 5: Validating data on the client using ColdFusion form tags 115
ColdFusion form tags include the following attributes:
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 cfform tag)
The following code is on the server (tripeditaction.cfm page):
<!--- Number of people 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>
Client-side validation approach using a cfform tag
The following code is on the client (tripedit.cfm page):
<cfinput name="duration" message="Duration must be a number and cannot be
blank."
validate="integer" required="Yes" size="3" maxlength="3">
Attribute Description
validate The data type that the field tag validates against. Values include: integer, date, time,
telephone, zipcode.
message The error message that appears 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.
Code Explanation
<cfinput name="duration"
message="Duration must be a number
and cannot be blank."
validate="integer"
required="Yes" size="3"
maxlength="3">
Use the cfinput tag to create the duration input entry
field within a
cfform tag. The validate attribute defines
the field as an
integer. The required attribute indicates
that the field must have an entry. If the data is not
entered or data entered is not an integer, the
message
attribute specifies that the message, “Duration must
be....” appears.