| By Selene Bainum | Article Rating: |
|
| July 26, 2000 12:00 AM EDT | Reads: |
21,405 |
Anyone who's needed JavaScript validation for his or her forms knows how easy it is to use ColdFusion's CFFORM tag. It's a quick and easy way to ensure that the form is filled out properly. However, CFFORM can be as limited as it is useful (see Figure 1).
Several types of validation that aren't available are:
- Making a single-select box required
- Making checkboxes and radio buttons required
- Limiting the number of checkboxes or multiselect options selected
- Restricting dates entered to a certain range.
It's possible to use both - good news for anyone who already uses CFFORM and wants to add on to it or for those who're tired of writing JavaScript to ensure basic validation.
Modular Validation Scripts
The CFFORM JavaScript validation is modular. There's a function for each type of validation. These functions are called by the main function for each form field that uses them. The validation to make sure a text box is filled will be called five times if you have 5 CFINPUT TYPE = "text" fields with REQUIRED set to yes. Our customized validation will be set up the same way.
First you need to create your SCRIPT tag. Place your <SCRIPT> tag inside the <HEAD> tags. ColdFusion positions the CFFORM JavaScript just above the </HEAD> tag. If your JavaScript isn't inside the <HEAD>, it'll be below the CFFORM JavaScript. It'll still work, but it's harder to debug your JavaScript when it's at line number 400 in your source code. It's much easier to have it at the top of your source. Your customized validation will be run after the CFFORM validation completes successfully.
<HTML>Your JavaScript will contain several functions. The one that processes when the form is submitted is the one that ultimately decides whether the form should indeed be submitted or if the action should be halted and an alert box returned to the user. This function will call your validation functions. You'll have one validation function for each type of validation you wish to perform. We're using ExtendJS() as the main function. Inside the parentheses after the function is the variable FormName. To make the code modular we're also passing in the form's name to the function; it will be represented in the functions as the FormName variable.
<HEAD>
<TITLE>Page Title</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
function ExtendJS(FormName) {
...
}
</SCRIPT>
</HEAD>
Calling Your Scripts
For this function to be processed when the form is submitted, you must add an onSubmit method to the CFFORM tag:
<CFFORM NAME = "Extend" ACTION =In your function call you need to include the name of your FORM. This is the value of the NAME attribute of the CFFORM tag. In the example above the name of the form is Extend.
"Action.cfm" onSubmit = "return
ExtendJS('Extend')">
Now we can start adding validation functions and then calling them from our main function.
Two of the most common validations CFFORM lacks are making a single-select box required (really required, not just passing the first value) and making a text area field required.
Required Select Boxes
Unless an option is set to be selected when the page loads, the first item in a single-select box is selected by default. You must have a value selected in a single-select box. When using a CFSELECT populated by a query, the first value from the query will be selected automatically. If the user doesn't make any changes, that first value will be passed to the action page. There's no way to make sure the user actually chooses an item. To really make a single-select box required you must use the HTML SELECT tag and then have the options generated inside a CFOUTPUT tag. Before that CFOUTPUT tag, however, include an empty option tag. This empty option is what will be selected by default:
<SELECT NAME = "SelectBox">You then need a function to ensure that a value is actually selected by the user. The first option in a select box has an index of 0. If that option is the one selected, you know the user hasn't really selected an option. In that case the function will return a value of false. If any other option is selected, the function will return a value of true. This function can be placed inside the same SCRIPT tag as your main function (ExtendJS), but it must be underneath or below it, not inside it.
<OPTION VALUE = "">Select One
<CFOUTPUT QUERY = "MyQuery">
<OPTION VALUE =
"#Field#">#Field#
</CFOUTPUT>
</SELECT>
functionTo use this validation for your select boxes you'll need to call the validation function from the ExtendJS function:
SingleSelectRequired(Form, Field) {
var itemSelected =
eval("document." + Form + "."
+ Field + ".selectedIndex");
if (itemSelected == 0) {
return false;
} else {
return true;
}
}
function ExtendJS(FormName) {
if (!SingleSelectRequired(FormName,
'SelectBox')) {
alert("You must select an
item from the drop-down
list.");
return false;
}
...
}
In the script above you're calling the SingleSelectRequired function and passing it the name of the form and the select box. To use the SingleSelectRequired function for multiple select boxes, call the function for each one and pass it the appropriate field name. If the function returns false, the alert message will display for the user and the form won't submit. If the function returns true, the script will continue running. If all validations pass, the form will be submitted.
Required Textarea Fields
One of the most commonly used form fields, TEXTAREA, doesn't even have a CF equivalent. This makes validating a TEXTAREA very difficult. To include a TEXTAREA in your validation, first add the field to your form:
<TEXTAREA NAME = "Comments" COLSNow add a validation function:
= "30" ROWS = "4" WRAP =
"virtual"></TEXTAREA>
function TextAreaRequired(Form,This validation function is slightly different than the one for a text box. Instead of just checking to see if the form field has a value, see if the form field's value has a length. If the length is 0, you know nothing was entered into the TEXTAREA. To call this function, add another call to your ExtendJS function:
Field) {
var length = eval("document." +
Form + "." + Field +
".value.length");
if (length == 0) {
return false;
} else {
return true;
}
}
if (!TextAreaRequired(FormName,Additional Tips
'Comments')) {
alert("You must enter some
comments.");
return false;
}
The two examples above show how to validate HTML FORM fields inside a CFFORM. You can also write your own functions to perform additional validation on your CFFORM tags. Listing 1 provides examples of performing custom validation on CFINPUT fields of type text, radio and checkbox. These validation scripts can be used for both CFML and HTML form fields. In your JavaScript simply pass the name of the form field as you would for HTML fields.
Published July 26, 2000 Reads 21,405
Copyright © 2000 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Selene Bainum
Selene Bainum is a software architect at INPUT. She has been a ColdFusion and SQL developer for over 10 years and runs www.webtricks.com.
![]() |
Joel 06/04/08 11:12:45 AM EDT | |||
Your CFFORM validation doesn't seem to work with CF8 -- when I try it.. the ONSUBMIT is rewritten by CF to be: onsubmit="return _CF_checkregistration(this) |
||||
![]() |
Drew Foster 07/20/07 01:34:47 PM EDT | |||
This is a great article and saved me from pulling out my hair any further. Works great too. Thanks so much. Another article that may be of interest is this (the last comment by Steve O in particular): And this: http://coldfusiononline.blogspot.com/ under CFFORM VALIDATION DOES NOT WORK. These articles might save someone else some stress, all articles (including yours) combined I was able to get my validation problems solved. Thanks again. |
||||
![]() |
06/11/02 10:23:00 AM EDT | |||
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe Reader Sued
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Adobe Flex Developer Earns $100K in New York City
- Adobe LiveCycle Enterprise Suite 2 for Cloud Computing
- Adobe Cans Another 9% of its Workforce
- Adobe Betas Target RIAs and Cloud Computing
- Adobe MAX 2009 Online
- Thinking of Flex in London
- Moyea DVD4Web Converter V2.0 Converts DVD to FLV Fast and Synchronously with Watermarks
- Adobe & Salesforce Cut Cloud Deal
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Eval JavaScript in a Global Context
- Fig Leaf Software to Exhibit at Government IT Conference & Expo
- Is Microsoft as Free as Open Source?
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe Reader Sued
- The Planet Named “Bronze Sponsor” of Cloud Computing Expo
- Microsoft Expression Web Has Got Game
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Bruce Chizen Joins Voyager Capital as Venture Partner
- My Top Seven Wishes From Adobe MAX 2009
- Adobe Flex Developer Earns $100K in New York City
- The Next Programming Models, RIAs and Composite Applications
- Where Are RIA Technologies Headed in 2008?
- Constructing an Application with Flash Forms from the Ground Up
- AJAX World RIA Conference & Expo Kicks Off in New York City
- CFEclipse: The Developer's IDE, Eclipse For ColdFusion
- Personal Branding Checklist
- Adobe Flex 2: Advanced DataGrid
- Has the Technology Bounceback Begun?
- Building a Zip Code Proximity Search with ColdFusion
- i-Technology Viewpoint: We Need Not More Frameworks, But Better Programmers
- The Asynchronous CFML Gateway
- Web Services Using ColdFusion and Apache CXF




































