000907.html | WebReference


((((((((((((((((( WEBREFERENCE UPDATE NEWSLETTER ))))))))))))))))) September 7, 2000


This newsletter sponsored by: Searchbutton, Allaire and Recommend-it __________________________________________________________________


Help your Web site visitors find what they're looking for - add site search today!


The preferred solution recognized by webmasters worldwide.


http://www.webreference.com http://www.webreference.com/new/ http://www.webreference.com/new/submit.html New this week on WebReference.com and the Web:

1. OPEN PUBLISHING: Submit Your Article Today! 2. FEATURED ARTICLE: Form Validation with Cold Fusion and JavaScript 3. NET NEWS: * Congress Takes up Bill on Email Monitoring * IBM to Unveil New Wireless Devices * Amazon Determining Discounts Dynamically? * WAP Services For Cattle Safety

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. OPEN PUBLISHING: Submit Your Article Today!

Every Thursday the Update features a new article contributed by our readers through our Open Publishing Initiative. We encourage you to submit your own article ideas. Your words could be here!


This week, Joe Copley demonstrates a simple scripting solution to validating Cold Fusion forms with JavaScript. This "best-of-both- worlds" approach allows you to leapfrog limitations inherent in both technologies.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 2. FEATURED ARTICLE: Form Validation with Cold Fusion and JavaScript

Web programming with Cold Fusion, or any language, almost always involves forms. Where there are forms, there must be input validation. CF supports server-side form validation, but not very efficiently. Because of this, most developers supplement their forms with client-side JavaScript validation.

This article presents a script that provides automatic JavaScript validation for Cold Fusion-validated forms. Use it to get the best of Cold Fusion and JavaScript validation, without duplicating your validation logic.

>Cold Fusion Validation

Cold Fusion employs hidden form fields to validate the contents of user-entered data. Here is a hidden field that ensures that a text box is not empty:

<input type="hidden" name="companyName_required" VALUE="Please provide a company name">

The name of the field determines the actual form element being validated and the type of validation required. The field's value contains the message to be displayed if validation fails. In this case, Cold Fusion will require the form element 'companyName' to contain a value. Other types of validation are supported for most kinds of available form elements.

CF validation will get the job done, but it requires extra trips to the server when validation errors occur. While you can specify a validation message, it will be displayed on a rather pedestrian page, no matter how elegantly your site is designed. The user must press the Back button to return to the form to make corrections, and in some cases, this will wipe out the contents of the form. I can't think of a better way to annoy a user.

>JavaScript Validation

JavaScript, on the other hand, seems well suited for validation tasks. Validation occurs on the client-side, not the server, so it is efficient. When validation errors occur, an alert box is displayed, and the user stays on the form that needs fixing, instead of going to an error page.

JavaScript also has one whopper of a drawback: it can be disabled by the user. Granted, few users will do so, but since validation is the science of dealing with exceptions, if you can't achieve 100% validation, you lose the whole ball game.


Learn why Allaire ColdFusionR is the fastest way to develop and deliver scalable Web applications. Join us for a FREE, introductory, one-hour CyberSeminar on the industry's leading cross-platform Web application server. You may win a FREE copy of ColdFusion Server Professional - a $1,295 value! Register today at http://www.allaire.com/cfcyber/webref


>The Best of Both

Because neither Cold Fusion nor JavaScript validation is ideal, using both makes sense. When I first starting doing this, I created separate JavaScript functions for each form. This quickly became tedious, all the more so because I was duplicating the CF validation logic.

The following script eliminated these problems. It can be reused without modification whenever you want to add client-side validation to your Cold Fusion forms.

function checkForm(thisform) { returnval=true; for (var j=0; j<(thisform.elements.length); j++) { indx = thisform.elements[j].name.indexOf('_required'); if (indx != -1) { fieldname=thisform.elements[j].name.substring(0,indx); if (thisform.elements[fieldname].value.length == 0) { alert(thisform.elements[j].value); j = (thisform.elements.length); returnval = false; } } } return returnval; }

You can paste this directly into any form that requires it, but I recommend using the SRC attribute of the SCRIPT tag. That way, all forms can access a single copy of the script.

The form tag is written as follows:

<form name="myForm" ACTION="formprocessor.cfm" METHOD="POST" onSubmit="return checkForm(this)">

When the form is submitted, it invokes the function and passes a reference to itself as a parameter. The function loops through all form elements looking for field names that end with '_required.'

If it finds such a field, it uses the beginning of its name to determine the name of the field to be validated, and then checks the field's value. If the value is empty, the script displays an alert, using the error message from the hidden CF validation field. It then returns the value false to cancel the submission of the form.

Once the script finds a validation error, it stops validating further by setting the loop counter j to its maximum value.

In this article I wanted to focus on the basic technique, so I've made the script very simple. For example, it validates text box values only. However, Cold Fusion supports several different kinds of validation (e.g., float, integer, range, etc.); with modifications, the script can support them as well.

You may also want to support validation of fields other than text boxes, but here things get tricky. First, drop-down fields don't have a value attribute in Netscape 4.x, so you have to use different logic to validate them. Likewise, checkboxes and radio buttons require special handling. The problem then becomes recognizing different types of fields. The JavaScript type operator provides this information in modern browsers, but is not supported by old browsers. An alternative is to use object detection techniques to figure out field types. Consult the Webreference.com JavaScript section http://webreference.com/js/ for an excellent article on this subject.

Another complication occurs when your form has multiple fields with the same name. The script can be modified to support them, but that is a subject for another article.


When it comes to programming, anything worth doing is worth doing exactly once. This script can help you get the best of Cold Fusion and JavaScript validation, without doubling your programming load or your maintenance headaches.


Cash in on increased traffic with Recommend-it! Recommend-it provides web audiences an easy, intuitive way to refer their network of friends to your site. Take advantage of the largest referral service on the Internet and see why over 90,000 Webmasters and millions of people have decided to Recommend-it!



About the author:

Joe Copley is president of Copley Internet Systems http://www.copleyinternet.com. His firm is based in Charlotte, NC and specializes in e-commerce sites using Cold Fusion. Contact him at joe@copleyinternet.com.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3. NET NEWS: Congress Takes up Bill on Email Monitoring, IBM to Unveil New Wireless Devices, Amazon Determining Discounts Dynamically?, WAP Services For Cattle Safety

>Congress Takes up Bill on Email Monitoring

A bill that would prohibit companies from secretly monitoring their employees' email messages and Internet usage could get key congressional subcommittee approval next week. http://www.idg.net/ic_240805_1794_9-10000.html IDG.net, 000907 >IBM to Unveil New Wireless Devices

IBM will show that it is not messing around in the overwhelming tide of wireless growth this week at the DEMOmobile conference. The new IBM ThinkPad I Series notebook computers will be the first Intel processor-based systems to offer integrated 802.11b wireless LAN capabilities. http://www.internetnews.com/prod-news/article/0,,9_454331,00.html InternetNews.com, 000907

>Amazon Determining Discounts Dynamically?

Consumers who shop for DVD's on Amazon.com's Web site could be charged as much as $10 more than other customers purchasing the same product at approximately the same time - a practice the company described as a periodic test that it runs on the prices of certain items. http://www.computerworld.com/cwi/story/0,1199,NAV47_STO49569,00.html ComputerWorld.com, 000905

>WAP Services For Cattle Safety

A new Wireless Application Protocol service to be launched by agricultural Web site Braidgrove.com in the coming weeks will allow livestock traders to accurately map the location of British beef across the country. No word on when they'll start fitting the sheepdogs with pagers... http://www.zdnet.com/zdnn/stories/news/0,4586,2625118,00.html ZDNet.com, 000907

That's it for this week, see you next time.

Andrew King Managing Editor, WebReference.com update@webreference.com

Eric Cook Assistant Editor, WebReference.com ecook@internet.com

Catherine Levy Assistant Editor, WebReference.com clevy@internet.com

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For information about advertising in this newsletter, contact Frank Fazio, Director of Inside Sales, Jupitermedia Corp. Call (203)662-2997 or write ffazio@internet.com ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This newsletter is published by Jupitermedia Corp. http://internet.com - The Internet Industry Portal ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ To learn about other free newsletters offered by internet.com or to change your subscription - http://e-newsletters.internet.com ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ internet.com's network of more than 100 Websites are organized into 14 channels... Internet Technology http://internet.com/sections/it.html E-Commerce/Marketing http://internet.com/sections/marketing.html Web Developer http://internet.com/sections/webdev.html Windows Internet Technology http://internet.com/sections/win.html Linux/Open Source http://internet.com/sections/linux.html Internet Resources http://internet.com/sections/resources.html Internet Lists http://internet.com/sections/lists.html ISP Resources http://internet.com/sections/isp.html Downloads http://internet.com/sections/downloads.html International http://internet.com/sections/international.html Internet News http://internet.com/sections/news.html Internet Investing http://www.internet.com/sections/stocks.html ASP Resources http://internet.com/sections/asp.html Wireless Internet http://internet.com/sections/wireless.html ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ To find an answer - http://search.internet.com ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ For information on reprinting or linking to internet.com content: http://internet.com/corporate/permissions.html ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Copyright (c) 2000 Jupitermedia Corp. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~