Web Validation and hidden fields

I’m doing validation with javascript for a form on the web through a submit button. Now one of the fields is only displayed whether or not another field has a specific value. (Example: If the value chosen in the picklist of field1 contains the string ‘D.O.’, show the hidden field2.)

I need to perform validation through javascript on field 2 if its displayed. But if its hidden, of course the debugger gives an error saying its a null object. How do I get around this?

Subject: Web Validation and hidden fields

You already gave half of the answer: JavaScript provides a very easy means to test, if an object is undefined or Null.

if (yourObject) {

// your usual code here

}

where yourObject is the object referring to the field (however you get it right now, e.g. var yourObject = document.forms[0].YourField;).