Validating Form Data Using CFFORM ColdFusion Help

Nothing that you have seen so far in this chapter requires the use of ColdFusion-spedfic capabilities. For instance, all the server-side validation you did could just as easily have been done in Perl, JavaScript, or C++. Similarly, the [avaScript-based client-side validation you did requires no use of ColdFusion.

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
Enhancing validation with JavaScript

Ensuring That Required Fields Are Filled In

All the ColdFusion form tags (CFINPUT, CFSELECT, CFTABLE, and so on) with the exception of CFGRID and CFSLIDER provide two attributes that allow client-side validation of ;equired fields. These attributes are REQUUED and MESSAGE. Much like our attempts at JavaScript code to ensure that required fields are filled in, these attributes cause ColdFusion to generate the JavaScript code needed to validate any and all required form fields. Let’s start with our original form converted to ColdFusion tags, as shown in Listing 16.6.

cfform1.dm

 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.

cfform2.cfrn

When this form is used, attempts to submit the form without filling the reqclred fields generates JavaScript alert dialog boxes like the following one.

‘However, the default message used by the ColdFusion-generated JavaScript functions doesn’t provide much useful information. You can adjust this with the MESSAGE attribute. You use this function to specify an alternative error message tctthe default one provided by ColdFusion:

<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.

Using the VALIDATE attribute implements client-side JavaScript validation just as using REQUIRED does. In fact, it uses the same default message for failure as does REQUIRED I which can be overridden with the MESSAGE attribute. As an example, consider the Phone field in your form. If you extend your specification to require a full U.S.-style phone number with an area code, you can use ColdFusion to implement format verification by adjusting your CFINPUT tag accordingly:

<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.

Posted on November 14, 2015 in Validating From Data

Share the Story

Back to Top
Share This