Form ValidationValidation refers to the process of checking the contents of a user-submitted form before processing it. Most modern web forms contain some sort of validation to ensure that good data is passed through the form and all required fields are completed. Client Side ValidationClient side validation means that the form's contents are checked by a script that runs on the user's computer (typically JavaScript). ColdFusion's <cfinput> tag allows you to easily and automatically generate client side validation. Example:
<cfinput name="mail_name" type="text" size="50" required="yes" message="Please enter your name">
Note that the 'required' attribute is set to 'yes', and the 'message' attribute contains the error message that should be displayed if the field is left empty.
Drawbacks to client side validationThe major problem with client side validation is that it relies on the user's computer to perform the validation. However, if a user has disabled JavaScript on their browser, they can bypass this client side validation. Therefore, it is a good idea to use client side validation in conjunction with server side validation, as explained below. Server Side ValidationServer side validation means that the form data is passed to the web server, and a script is run to validate its contents on the web server. This is an important advantage over client side validation because the user can not disable it. Example
<cfinput name="mail_name" type="text" size="50">
As you can see, a second <input> tag is used to trigger the validation. The 'type' attribute MUST be set to 'hidden'. The 'value' attribute is optional, and should be set to whatever error message you would like the user to see if the field is not filled in.
<input name="mail_name_required" type="hidden" value="Please enter your name."/> A best practice: use them both!It is considered best practice to use both client and server side validation. Although server side validation might be adequate by itself, relying on only server side validation means extra work for the web server, since every request MUST be sent to the server and processed. Since the majority of users have JavaScript enabled, client side validation will work just fine for them without having to use the web server for each form submission. By using both, you can make sure the form contains valid data and minimize the work the web server has to do. Example:
<cfinput name="mail_name" type="text" size="50" required="yes" message="Please enter your name">
Remember to use <cfform> and </cfform> instead of <form> and </form>, or you will get an error message and your form won't work. Also, as with any page
that uses ColdFusion, be sure to give the file the extension .cfm instead of .htm or .html.
<input name="mail_name_required" type="hidden" value="Please enter your name."/> |
Control PanelAccess your website, update you contact information, change your password. Login now! Volunteer WebmastersThings to IncludeResources and Downloads
Images Templates Get HelpIf you are having difficulty with any of our services or tools, or if you are just plain stuck on something, please feel free to contact our office. We can help you with your HTML, CSS, and Coldfusion, and we may be able to help you get your audio, videos, images, and flash up and running as well. Send an email to Assistant Webmaster, Adam Palcich, at app35@cornell.edu. |