To give focus to an object, use the focus() method. Thus I would try the following....
1) Have a variable that hold the name of the element that errors. (eg errObj)
2) In the first loop where you check "_required", just set the variable errObj to the name of the element that was empty.
3) Then in the last check, call:
Code:
if (boolIsValid) { ... } else {
alert( errMessage );
document.getElementById(errObj).focus();
return false;
}
A few things to note, however....
1) As written, this would actually return the user to the last error, since objErr would keep being overwritten for each error. A way around that would be to "break;" out of the loop if one of the required fields was empty (this would also tend to speed up the script since it would stop going through the form that it already "knew" was wrong).
2) Since you'd be using objErr, the existence of anything inside the variable would indicate that an error had occured; hence it could be used in place of boolIsValid (eg "if (objErr=='')...").
3) If I remember correctly, IE has a problem with going to an element using focus() directly. It needs a slight delay to finish the function before changing the focus. Thus you may need to change the last step to:
Code:
setTimeout("document.getElementById(errObj).focus();", 100);
This will give a tenth-of-a-second delay which should be enough time to finish the function without having the user notice the delay.