1. Home
  2. Computing & Technology
  3. JavaScript

Introduction to Form Field Validation

How Much Validation is Required?

By Stephen Chapman, About.com

Let's start by considering those field types that never or rarely require validation. The reset button is used to clear the entire content of the form, that is its defined function and it doesn't need to have anything attached to the button to get it to do that. The reset button is one that you should never consider validating.

The other buttons including image buttons and the submit button - which also has the special function of executing the form action - are used to trigger form events. You will probably attach an onclick event handler to these buttons to call a validation routine to validate the entire form content but the buttons themselves do not require validation.

Check boxes have two states - either they are checked or they are not. You don't need to validate that a check box is checked or unchecked, you just need to process the checked value for true or false to see which way the checkbox is set. A checkbox may need to be cross validated with other fields that may or may not be allowed to be entered or have certain values depending on the state of the checkbox.

Radio buttons are the simplest fields that actually require validation. The validation that is required is to test to make sure that one of the radio buttons in a group of radio buttons is actually selected. Some people avoid the need to test for this by defining one of the buttons in the group as checked="checked" to begin with but this may mean that the wrong value is being passed by visitors who neglect to select the correct button. A radio button group may also need to be cross validated with other fields.

Drop down lists automatically have the first entry selected by default. These fields do not require validation unless the list contains entries such as a blank default or separators between groups of entries. If the list does contain such entries then those entries should all be assigned one unique value that is not a value used by the valid entries. That unique value can then be tested for and if found then the field is not valid. Drop down lists may also need to be cross validated with other fields.

Password fields are a specialized form of text fields where the field displays asterisks instead of whatever is typed. As such the same validations might apply to password fields as for text fields however since the fields are almost exclusively used for passwords the actual validations required will not be very involved.

Input fields and input areas are the fields that require the most complex validations since they are the ones where the user can enter whatever they want. With text areas they aren't even limited to one line of so many characters but can enter as much as they like subject only to the validations that you apply. It is these validations that you will need to build one step at a time until you have added all of the tests needed to ensure that what is entered matches with what you require. In some instances the particular field content may be so common that you can use a routine already written that will perform all of the validations required (eg. for email address input).

Explore JavaScript

More from About.com

  1. Home
  2. Computing & Technology
  3. JavaScript
  4. Validating Forms
  5. How Much Validation is Required?

©2008 About.com, a part of The New York Times Company.

All rights reserved.