WebRef Update: Featured Article: Form Validation with Cold Fusion and JavaScript
|
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.
Next: The Best of Both Approaches
This article originally appeared in the September 7, 2000 edition of the WebReference Update Newsletter.
Comments are
welcome
Written by Joe Copley and
Revised: Sept. 8, 2000
URL: https://webreference.com/new/cfjsvalidate.html