Multiple Field Validation on Submit #2
Name:
E-Mail Address:
Address:
City:
State:
Zip:
Phone #:
Code:
validate.js
Highlight
function isBlank(strValue) { var intCnt; var strTemp; for(var intCnt = 0; intCnt < strValue.length; intCnt++) { strTemp = strValue.charAt(intCnt); if ((strTemp != ' ') && (strTemp != '\n') && (strTemp != '\t')) return false; } return true; } function hilite(objForm, objField, strAlertOn) { // This highlights/unhighlights the text box for vaigators above 3 //Match 'Explorer' to navigator name. Ignore case var anyString=navigator.appName; if(parseInt(navigator.appVersion)>3 && anyString.indexOf('Explorer')>0) { if(strAlertOn) objForm.elements[objField].style.backgroundColor="ff0000"; else objForm.elements[objField].style.backgroundColor="ffffff"; } } function SelectCheck(check,blnValue) { if(blnValue) check.checked=true; else check.checked=false; } function VerifyInput(objForm) { var strFirstErr = ''; var strMsg = ''; // ------------------------------------------- if(isBlank(objForm.name.value)) { hilite(objForm,'name',true); if(strFirstErr=='') strFirstErr = 'name'; strMsg = strMsg + "> Name\n"; } else hilite(objForm,'name',false); // ------------------------------------------- if(isBlank(objForm.email.value)) { hilite(objForm,'email',true); if(strFirstErr=='') strFirstErr = 'email'; strMsg = strMsg + "> Email Address\n"; } else hilite(objForm,'email',false); // ------------------------------------------- if(isBlank(objForm.city.value)) { hilite(objForm,'city',true); if(strFirstErr=='') strFirstErr = 'city'; strMsg = strMsg + "> City\n"; } else hilite(objForm,'city',false); // ------------------------------------------- if(isBlank(objForm.state.value)) { hilite(objForm,'state',true); if(strFirstErr=='') strFirstErr = 'state'; strMsg = strMsg + "> State\n"; } else hilite(objForm,'state',false); // ------------------------------------------- // Did any fields contain invalid data? if(strMsg=='') objForm.submit(); else { alert("The following field(s) have errors:\n\n" + strMsg + '\n' + "Correct and try again."); objForm.elements[strFirstErr].focus(); } }
Place in the <HEAD>:
Highlight
Place in the <BODY>:
Highlight
Name:
E-Mail Address:
Address:
City:
State:
Zip:
Phone #: