Within a lengthy function (25 or more lines), this snippet provides an overview of the process. The intention is to validate each field in sequence, starting with the first one. If validation fails, an alert should pop up and focus on the problematic field.
The issue encountered is that instead of following the intended order, the validation process begins from the 'DateDue' field and moves backwards. Additionally, the alert message does not appear as expected.
This code resides in the JSHeader file and is triggered by the Submit button.
Any insights into what might be causing these discrepancies?
function validate(){
var validatemsg;
var validateflag;
validateflag = 'false';
if(document.forms[0].LocationName.value == ''){
validatemsg='LOCATION NAME FIELD ERROR: Location Name is required to successfully process your request.';
validateflag='true';
document.forms[0].LocationName.focus()
}
if(document.forms[0].LocPhone.value == '') {
validatemsg='LOCATION PHONE FIELD ERROR: Location Phone is required to successfully process your request.';
validateflag='true';
document.forms[0].LocPhone.focus()
}
if(document.forms[0].LocFax.value == '') {
validatemsg='LOCATION FAX FIELD ERROR: Location Fax is required to successfully process your request.';
validateflag='true';
document.forms[0].LocFax.focus()
}
// Around a dozen similar fields come next
if(document.forms[0].DateDue.value == '') {
validatemsg='DATE DUE FIELD ERROR: Date Due is required to successfully process your request.';
validateflag='true';
document.forms[0].DateDue.focus()
}
if(document.forms[0].DateDue.value.length != 10) {
validatemsg='DATE DUE FIELD ERROR: Date Due should be in mm/dd/yyyy format.';
validateflag='true';
document.forms[0].DateDue.focus()
}
if(document.forms[0].AgreeType.value == '') {
validatemsg='AGREEMENT TYPE FIELD ERROR: AgreementType is required to successfully process your request.';
validateflag='true';
document.forms[0].AgreeType.focus()
}
if(validateflag == 'true'){
alert(validatemsg);
}
if(validateflag == 'false'){
document.forms[0].submit()
}
}