1. Home
  2. Computing & Technology
  3. JavaScript

Cross Field Validation

clr gif
Join the Discussion

Questions? Comments?

Just because you have validated all of the individual fields on your form doesn't mean that you have finished validating the form. You know by this point in your validation that what has been entered into a specific field meets the validation rules that you have applied to that field. What you don't know is if the combination of what has been entered into different fields makes sense.

What we need to do to finish validating our form is to cross validate the fields in our form to compare the values entered into two or more fields in our form to see if their contents can be considered valid in combination.

Cross validation of fields is done in the form validation function that is called when the form is submitted. It can't be attached to individual fields because more than one field is involved and until the form is ready to be submitted we don't know if the other field has been updated yet. The cross validation code also should follow calls to all of the individual field validation functions as there is no point in checking if multiple fields in combination are valid until after you have determined that the fields contain valid information in the first place.

Cross field validations that involve check boxes, radio buttons, and/or drop down selection lists are particularly easy since there are only a limited number of field values all of which are defined in the form. Cross field validations involving multiple text fields are the most complex but even there most times the values you want to cross validate will usually only involve a small range of values.

For example. the simplest situation with cross field validation involving two text fields is where the two fields are mutually exclusive (ie. only one field should be entered).

if (fieldone.value != '' && fieldtwo.value != '')
return false;

If it is mandatory that one of these fields be entered then you can't just test each field separately as being mandatory since that would force values to be entered into both fields and it would fail the above test. Instead we need to apply the mandatory test as a cross field validation as well.

if (fieldone.value == '' && fieldtwo.value == '')
return false;

The only thing you need to watch for involving non-text fields is that check boxes don't have a value. Instead you need to test for whether they are checked or not. For example if a field is mandatory if a checkbox is checked then you'd use:

if (fieldone.checked && fieldtwo.value == '')
return false;

The actual if statements that you need may become more complicated involving combinations of || (or) conditions between possible values for the same field and && (and) conditions between different fields. It wont become more involved than that though provided that the cross field validation only involves comparisons of complete field values.

Only if the cross field validation involves comparing values in part of a text field will it progress to the point where you will need additional processing. In this case some of the code in some of the text field validations that you perform may need to be copied into a separate cross field validation function in order to extract the appropriate part of the field for the comparison.

Explore JavaScript
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

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

All rights reserved.