// -------------------------------------------------------------------------------- // Bill Morris // Wednesday, August 28, 2002 function isNumeric(intKeyCode) { return (intKeyCode>=48 && intKeyCode<=57 || intKeyCode>=96 && intKeyCode<=105); } // -------------------------------------------------------------------------------- function isDash(intKeyCode) { return (intKeyCode==189 || intKeyCode==109); } // -------------------------------------------------------------------------------- function isWhack(intKeyCode) { return (intKeyCode==111 || intKeyCode==191); } // -------------------------------------------------------------------------------- function isDot(intKeyCode) { return (intKeyCode==110 || intKeyCode==190); } // -------------------------------------------------------------------------------- // Bill Morris // 30 September, 2000 function popWindow (href,w,h, allowDebug){ if (allowDebug == 1){ popWin = window.open(href,"PopupWindow","menubar=yes, toolbars=yes,status=yes,scrollbars=yes,resizable=yes,width=" + w + ",height=" + h) } else{ popWin = window.open(href,"PopupWindow","toolbars=no,scrolling=no,resizable=no,width=" + w + ",height=" + h) } } // -------------------------------------------------------------------------------- // Bill Morris // 28 October, 2000 function validateDate (fieldname, mm, dd, yy, isRequired, dateType){ var errMsg = "" if(yy < 100){ yy = 1900 + yy } if (dateType != "small"){ if (yy < "1754" || yy > "3000"){ return fieldname + ' is not valid: check that the year is four digits\n' } } else { if (yy < "1900" || yy > "2079"){ return fieldname + ' is not valid: check that the year is four digits\n' } } // if date is required then there must be a value in every field if (isRequired && (mm.length == 0 || dd.length == 0 || yy == 0)){ errMsg = errMsg + fieldname + ": A date is required and must take the form of mm/dd/yyyy\n" return errMsg; } else { // if the date is not required and all the fields are blank, return nothing if (mm.length == 0 && dd.length == 0 && yy == 0) { return ""; } } // now that we're past all that, start validating field values if (mm < 1 || mm > 12){ errMsg = errMsg + fieldname + ": The month value is invalid\n" } // alert (fieldname + '\n' + dd + '\n' + mmInfo[mm] + '\nLess than 1 = ' + (dd < 1) + '\nGreater than limit = ' + (dd > mmInfo[mm].value)) // if (dd < 1 || dd > mmInfo[mm].value){ if (dd < 1 || dd > 31){ errMsg = errMsg + fieldname + ": The day value is invalid\n" } if (yy.length < 4){ errMsg = errMsg + fieldname + ": The year must be four digits long\n" } return errMsg } // -------------------------------------------------------------------------------- // Bill Morris // 28 October, 2000 function displayError (errMsg){ var tmp = ""; tmp = "The following errors were encountered while processing the form.\n\n" + errMsg; tmp = tmp + "\nPlease check your values and re-key."; alert (tmp); } // -------------------------------------------------------------------------------- // Bill Morris // 28 October, 2000 function validateDate1 (fieldname, mm, dd, yy, isRequired, fieldObject, dateType){ var errMsg = "" // if the date is not required and all the fields are blank, return nothing if (!(isRequired) && mm == "" && dd == "" && yy == "") { //alert ("hi") fieldObject.value = "" return ""; } if (dateType != "small"){ if (yy < "1754" || yy > "3000"){ return 'The year for ' + fieldname + ' must fall between 1754 and 3000\n' } } else{ if (yy < "1900" || yy > "2079"){ return 'The year for ' + fieldname + ' must fall between 1900 and 2079\n' } } // if date is required then there must be a value in every field if (isRequired && (mm.length == 0 || dd.length == 0 || yy == 0)){ return fieldname + ": A date is required and must take the form of mm/dd/yyyy\n" } // now that we're past all that, start validating field values if ((mm < 1 || mm > 12) || mm == ""){ errMsg = errMsg + fieldname + ": The month value is invalid\n" } if (parseInt(mm)!=mm){ errMsg = errMsg + fieldname + ": The month value contains invalid characters\n" } if (dd < 1 || dd > 31 || dd ==""){ errMsg = errMsg + fieldname + ": The day value is invalid\n" } if (parseInt(dd)!=dd){ errMsg = errMsg + fieldname + ": The day value contains invalid characters\n" } if (yy.length < 4){ errMsg = errMsg + fieldname + ": The year must be four digits long\n" } if (parseInt(yy)!=yy){ errMsg = errMsg + fieldname + ": The year value contains invalid characters\n" } if (errMsg==""){ fieldObject.value=mm + '/' + dd + '/' + yy } else{ fieldObject.value="" } return errMsg } // -------------------------------------------------------------------------------- function validateField(fieldname, fieldObject, required, mode) { /* mode specifies what sort of data, numeric; decimal; email, etc if field is required, we test for to see if there is a value. If field is not required, then the only thing that invalidates the field is incorrect characters */ // alert (mode) // note: there is no test for "text" mode. If text, the only thing we need to check is if it's required. var invalidChars = " /:,;" var errMsg="" var invalidValue=false; var isDroplist isDroplist = false if(mode == 'droplist' || mode == 'dropdown' || mode == 'select') isDroplist = true if (required){ // if the value is required and blank, no further tests are required if (fieldObject.value == ""){ return fieldname + ' must have a value.\n' } } else { if(!isDroplist){ if (fieldObject.value == ""){ return ''; } } } if(!isDroplist){ switch(mode) { case 'numeric': errMsg += validateNumeric(fieldname, fieldObject); break; case 'date1': var tmpDate = fieldObject.value; tmpDate.replace(/\./g, '/'); tmpDate.replace(/-/g, '/'); tmpDate.replace(/\\/g, '/'); var newDate = tmpDate.split('/') errMsg += validateDate1 (fieldname, newDate[0], newDate[1], newDate[2], required, fieldObject, 'notSmall'); break; case 'date': var tmpDate = fieldObject.value; tmpDate.replace(/\./g, '/'); tmpDate.replace(/-/g, '/'); tmpDate.replace(/\\/g, '/'); var newDate = tmpDate.split('/') errMsg += validateDate(fieldname, newDate[0], newDate[1], newDate[2], required, fieldObject, 'notSmall'); break; case 'decimal': errMsg += validateDecimal(fieldname, fieldObject); break; case 'email': errMsg += validateEmail(fieldname, fieldObject); break; case 'phone': errMsg += validatePhone(fieldname, fieldObject, required, mode); break; case 'zip': errMsg += validateZip(fieldname, fieldObject, required); break; case 'ssn': errMsg += js_validateSSN(fieldname, fieldObject, required); break; default: // nothing to do break; } } else { errMsg += validateDroplist(fieldname, fieldObject) } return errMsg; } // -------------------------------------------------------------------------------- function validateFieldEx(fieldname, required, mode, doColor, returnMsg){ var errMsg = validateField(fieldname, document.getElementById(fieldname), required, mode) if(errMsg != '') { if(doColor != ''){ document.getElementById(fieldname).style.background = doColor; } if(returnMsg) { return returnMsg; } else { return errMsg; } } else { document.getElementById(fieldname).style.background = ''; return ''; } } // -------------------------------------------------------------------------------- function js_validateSSN(fieldname, fieldObject, required) { var toTest = fieldObject.value toTest = toTest.replace(/-/g, ''); if(isNaN(toTest)){ return('' + fieldname + ' does not appear to be valid\n') } if(toTest.length != 9){ return('' + fieldname + ' is not a valid length\n') } return ''; } // -------------------------------------------------------------------------------- function validateZip(fieldname, fieldObject, required){ var invalidValue=false; // first, we'll check to see if there's a value that just doesn't work... for (i = 0; i < fieldObject.value.length; i++){ if ((fieldObject.value.charAt(i) < "0" || fieldObject.value.charAt(i) > "9") && fieldObject.value.charAt(i) != "-"){ return fieldname + " contains invalid characters\n" } } if (invalidValue == false){ //we're okay so far, so continue checking if (required){ if (fieldObject.value == "-"){ return fieldname + ' can\'t just be a dash.\n' } if (fieldObject.value.length != 10 && fieldObject.value.length != 6 && fieldObject.value.length != 5){ return fieldname + ' is invalid.\n' } } else { // not required. Let's see if there's anything in the fieldobject if (fieldObject.value.length > 0 && fieldObject.value != "-"){ if (fieldObject.value.length != 10 && fieldObject.value.length != 6){ return fieldname + ' is invalid.\n' } } else{ fieldObject.value="" } } } return '' } // -------------------------------------------------------------------------------- function validatePhone(fieldname, fieldObject, required, mode){ var invalidValue=false; // assume that the value is good. var ctlValue = fieldObject.value if(ctlValue.length == 0){ if(required){ return fieldname + ' is missing' } else { return fieldname + '' } } // first, check for invalid characters, which will break the function whether the item is required or not for (i = 0; i < ctlValue.length; i++){ if ((ctlValue.charAt(i) < "0" || ctlValue.charAt(i) > "9") && ctlValue.charAt(i) != "-"){ return fieldname + ' contains invalid characters\n' } } // for var phoneValue = fieldObject.value var newPhoneValue = ''; // remove anything that isn't a number for (i = 0; i < phoneValue.length; i++){ newPhoneValue += (isNaN(phoneValue.charAt(i))) ? '' : phoneValue.charAt(i); } // for var aPhone = []; if(newPhoneValue.length == 7){ aPhone[0] = '' // newPhoneValue.substring(0,3) aPhone[1] = newPhoneValue.substring(3,6) aPhone[2] = newPhoneValue.substring(6,50) } else if(newPhoneValue.length >= 10) { aPhone[0] = newPhoneValue.substring(0,3) aPhone[1] = newPhoneValue.substring(3,6) aPhone[2] = newPhoneValue.substring(6,50) } else { return fieldname + ' is not a valid phone number\n'; } if (required){ // make sure we have an area code if (aPhone[0].length != 3 || aPhone[1].length != 3 || aPhone[2].length < 4){ return fieldname + ' is not a valid number\n'; } } //required // if the field isn't required, but there's a value in each, hit the function again as if the field is required else { if (aPhone[0].length != 0 || aPhone[1].length != 0 || aPhone[2].length != 0){ return validateField(fieldname, fieldObject,true, mode) } else{ fieldObject.value = "" } } //else required return '' } // -------------------------------------------------------------------------------- function validateEmail (fieldname, fieldObject){ var invalidChars = " /:,;" var invalidValue=true; // check for the @ symbol for (i = 0; i < fieldObject.value.length; i++){ if (fieldObject.value.charAt(i) == "@"){ invalidValue=false; } } invalidValue=true; // check for a dot for (i = 0; i < fieldObject.value.length; i++){ if (fieldObject.value.charAt(i) == "."){ invalidValue=false; } } // check for invalid characters for (i = 0; i < invalidChars.length; i++){ badChar=invalidChars.charAt(i); if (fieldObject.value.indexOf(badChar,0) > -1) { invalidValue=true; } } if (invalidValue){ return fieldname + ' is not a valid email address or contains invalid characters.\n' } return '' } // -------------------------------------------------------------------------------- function validateDecimal (fieldname, fieldObject){ var invalidValue=false; // initialize the value for (i = 0; i < fieldObject.value.length; i++){ if (fieldObject.value.charAt(i) < "0" || fieldObject.value.charAt(i) > "9"){ if (fieldObject.value.charAt(i) != "."){ return fieldname + ' must be numeric or decimal.\n' } } } return '' } // -------------------------------------------------------------------------------- function validateNumeric (fieldname, fieldObject) { var invalidValue = false; for (i = 0; i < fieldObject.value.length; i++){ if (fieldObject.value.charAt(i) < "0" || fieldObject.value.charAt(i) > "9"){ return fieldname + ' must be numeric.\n' } } return '' } // -------------------------------------------------------------------------------- // Bill Morris // December 28, 2000 function Date1GTDate2(y1, m1, d1, y2, m2, d2){ // compares two dates // returns true if second date is <= first date var SECOND = 1000 var MINUTE = SECOND * 60; // the number of milliseconds in a minute var HOUR = MINUTE * 60; // the number of milliseconds in an hour var DAY = HOUR * 24; // the number of milliseconds in a day var WEEK = DAY * 7; // the number of milliseconds in a week var Time1 = Date.UTC(y1, m1 -1, d1) var Time2 = Date.UTC(y2, m2 -1, d2); // specified time (UTC) var bTime = (Time1 - Time2) // time difference return (( bTime / DAY ) < 0); } // -------------------------------------------------------------------------------- function validateDroplist(fieldname, fieldObject) { for(i = 0; i < fieldObject.options.length; i++){ if(fieldObject.options[i].selected){ return ''; } } return 'Please make a selection for ' + fieldname; } // -------------------------------------------------------------------------------- function getMultipleChoiceValue(fieldObject){ var ctl if(fieldObject.options) { ctl = fieldObject.options for(i = 0; i < ctl.length; i++){ if(ctl[i].selected){ return ctl[i].value; } } } else { if (fieldObject.length) { ctl = fieldObject for(i = 0; i < ctl.length; i++){ if(ctl[i].checked){ return ctl[i].value; } } } } return '' } // -------------------------------------------------------------------------------- function makeReplace(haystack, needle, straw){ haystack.replace(eval('/' + needle + '/gi'), straw); return haystack; } //------------------------------------------------------------ function validateTwoPhones(args){ // DATE: Monday, January 28, 2008 // PURPOSE: validates two phone numbers // ASSUMPTIONS: at least one phone number is required // either number, if present, must be valid // validates for a ten digit number (area code, exchange, phone id, numbers only) // RETURN FLAGS: 0: function successful // 2: both numbers blank. Must have at least one // 4: phone 1 is invalid // 8: phone 2 is invalid // USAGE: var parms = [] // parms.retVal = 0; //output // // parms.phone1Ctl = []; // set up the two phone objects // parms.phone1Ctl.ctl = document.forms('form1').phone1; // parms.phone1Ctl.nm = 'Sample Phone'; // parms.phone1Ctl.length = 0; // output // parms.phone1Ctl.retMsg = ''; // output // // parms.phone2Ctl = []; // set up the two phone objects // parms.phone2Ctl.ctl = document.forms('form1').phone2; // parms.phone2Ctl.nm = 'Sample Phone'; // parms.phone2Ctl.length = 0; // output // parms.phone2Ctl.retMsg = ''; // output // // validateTwoPhones(parms) // // if(parms.retVal & 2) errMsg += '
Please enter at least one number'; // if(parms.retVal & 4) errMsg += '
Sample phone 1 is invalid'; // if(parms.retVal & 8) errMsg += '
Sample phone 2 is invalid'; var sTemp = ''; var phone1 = args.phone1Ctl.ctl; var name1 = args.phone1Ctl.nm; var phone2 = args.phone2Ctl.ctl; var name2 = args.phone2Ctl.nm; var phone1Val = jsTrimAll(phone1.value); var phone2Val = jsTrimAll(phone2.value); // at this point we know we have at least one phone value, // so now we have to validate for each // validate phone1 var validPhoneValues = '1234567890()-.' for(var i = 0; i < phone1Val.length; i++){ if(validPhoneValues.indexOf(phone1Val.charAt(i)) > -1){ // if we have a valid character AND it's a number, record it sTemp += (isNaN(phone1Val.charAt(i))) ? '' : phone1Val.charAt(i); } else { if(phone1Val.charAt(i) == ' '){ // do nothing } else { // got a bad character, so invalidate the number sTemp = 'x'; } } } if(sTemp.length < 10 && sTemp.length > 0){ args.phone1Ctl.length = sTemp.length; args.phone1Ctl.retMsg = '
' + name1 + ' is invalid or needs and area code'; args.retVal = args.retVal | 4; } else { phone1Val = sTemp } // validate phone2 sTemp = ''; for(var i = 0; i < phone2Val.length; i++){ if(validPhoneValues.indexOf(phone2Val.charAt(i)) > -1){ // if we have a valid character AND it's a number, record it sTemp += (isNaN(phone2Val.charAt(i))) ? '' : phone2Val.charAt(i); } else { // got a bad character, so invalidate the number sTemp = 'x'; } } if(sTemp.length < 10 && sTemp.length > 0){ args.phone2Ctl.length = sTemp.length; args.phone2Ctl.retMsg = '
' + name2 + ' is invalid or needs and area code'; args.retVal = args.retVal | 8; } else { phone2Val = sTemp } if(phone1Val.length == 0 && phone2Val.length == 0){ args.retVal = args.retVal | 2; } } //------------------------------------------------------------ function jsTrimAll(sString) { while (sString.substring(0,1) == ' '){ sString = sString.substring(1, sString.length); } while (sString.substring(sString.length-1, sString.length) == ' '){ sString = sString.substring(0,sString.length-1); } return sString; } //------------------------------------------------------------ function validateTwoPhones_showHelp() { document.write("

") document.write("var parms = []
") document.write("parms.retVal = 0; // output

") document.write("parms.phone1Ctl = []; // set up the two phone objects
") document.write("parms.phone1Ctl.ctl = document.forms['form1'].phone1;
") document.write("parms.phone1Ctl.nm = 'Sample Phone';
") document.write("parms.phone1Ctl.length = 0; // output
") document.write("parms.phone1Ctl.retMsg = ''; // output

") document.write("parms.phone2Ctl = []; // set up the two phone objects
") document.write("parms.phone2Ctl.ctl = document.forms['form1'].phone2;
") document.write("parms.phone2Ctl.nm = 'Sample Phone';
") document.write("parms.phone2Ctl.length = 0; // output
") document.write("parms.phone2Ctl.retMsg = ''; // output

") document.write("validateTwoPhones(parms)

") document.write("if(parms.retVal & 2) errMsg += '<br>Please enter at least one number';
") document.write("if(parms.retVal & 4) errMsg += '<br>Sample phone 1 is invalid';
") document.write("if(parms.retVal & 8) errMsg += '<br>Sample phone 2 is invalid';
") document.write("


") } //------------------------------------------------------------ function loadDateSelects(vDate, vTime){ if(vDate) loadDateSelects_date(vDate); if(vTime) loadDateSelects_time(vTime); }