Validating a large Web form

Opinion
Jan 24, 20051 min

* Dr. Internet columnist Steve Blass offers advice on how to validate a large Web form

We have a large Web survey with 100 radio button groups. How can we validate (using JavaScript) before the survey is submitted?

To ensure every radio button group in a form has an item selected, step through the form fields, track whether a radio group is found, and then verify that every found radio group has a selection. This code snippet is one way to implement this for a form named “rform.”function validate_radio () {

 var errors, i, itemname, fieldokflag=false, errflag=false, ,ffi;

 for (i=0;i

 itemname=document.rform.elements[i].name;

 if (document.rform.elements[i].type == ‘radio’) {

fieldokflag=false;

// assume radio group fields are in order

while (document.rform.elements[i].name == itemname) {

if ( document.rform.elements[i].checked ) {

fieldokflag=true;

}

i=i+1;

}

i=i-1; // while loop overcounted by 1, adjust

if (fieldokflag==false) {

errflag=true;

ffi=i-1; // ffi is index to last radio button in group

}

}

document.rform.elements[ffi].focus();

alert(‘Please complete the missing item(s) and then click Submit again’);

return (!errflag);

}