However, Coldfusion provides its own mechanisms for validating form data in Fusion-based forms built out of the CFFORMtag. This doesn’t mean that validation can be 100 percent automated with ColdFusion, but it can come close, especially when a form uses basic form elements such as text fields.
You will look at the following aspects of ColdFusion validation:
• Ensuring that required fields are filled in
• Ensuring that text fields contain properly formatted data
Ensuring That Required Fields Are Filled In
You can use the REQUIRED attribute to ensure that the required fields are filled in prior to form submission. This attribute can take the value YES or NO.In this case, you will use REQUIRED= H YES in both the Name and Phone fields, as shown in Listing 16.7.
<CFINPUT TYPE-TEXT NAME-‘Name’ SIZE-3D MAXLENGTH-SO REQUIRED-‘YES’ MESSAGE-‘Please enter a name. ‘>
The REQUIRED and MESSAGEattributes can be used with any of the ColdFusion form field tags.
Ensuring That Text Fields Contain Properly ~ormattedData.
When using Cold Fusion text fields (CFINPUT or CFTEXTINPUT), you can implement client- side validation of the data’s format in the fields. This is done using the VALIDATE attribute. This attribute takes one of the following values:
Date Validates field dat:l as a U.s. date in the format mm/dd/yy
EuroDate Validates field data as a European date in the format dd/mm/yyyy.
Time Validates field data as a time in the format hh:mm:ss.
Float Validates field data as a floating-point number,
Integer Validates field data as an integer number.
Telephone Validates field data as a U.S. phone number in the format XXX-XXX-xxXx or XXX XXX XXXX. The area code and the exchange must start with digits between 1 and 9.
Zipcode. Validates field data as a U.S. five-digit or nine-digit ZIP code in the format XXXXX, xxxxx – XXXX, or xx.xxx XXXX. (redi tcard Validates field data as a credit card number. Blanks or dashes can be included in the number.
Social_security typenumber Validates field data as a U.S. Social Security number in the format XXX-XX-XXXX or XXX XX XXXX.
<TD><CFINPUT TYPE-TEXT NAME-‘Phone’ SIZE-12 MAXLENGTH-12 REQUIRED-‘YES’ VALIDATE-‘Telephone’ MESSAGE-‘The phone number is either missing or invalid.’>
Because the same custom message is being used both when the field fails to be filled in and when it contains improperly formatted data, the text of the message has to be written in a way to convey both possible errors. The VALIDATE and REQUIRE validation failures do not have unique error messages.