// whitespace characters: ' ', '\t', '\r', '\n' var whitespace = " \t\n\r"; // isEmpty: Check whether string strVal is empty function isEmpty(strVal) { return ((strVal == null) || (strVal.length == 0)); } // isWhitespace: Check if string strVal has only the whitespace characters function isWhitespace(strVal) { var nPos = 0; if (isEmpty(strVal)) return false; for (nPos = 0; nPos < strVal.length; nPos++) if (whitespace.indexOf(strVal.charAt(nPos)) == -1) return false; return true; } // rtrim: Right trim the string and return the trimmed value function rtrim(strVal) { var nPos = 0; if (isEmpty(strVal)) return strVal; for (nPos = strVal.length-1; nPos >= 0; nPos--) { if (whitespace.indexOf(strVal.charAt(nPos)) == -1) break; } return (nPos == strVal.length-1 ? strVal.substring(0) : strVal.substring(0, nPos+1)); } // ltrim: Left trim the string and return the trimmed value function ltrim(strVal) { var nPos = 0; if (isEmpty(strVal)) return strVal; for (nPos = 0; nPos < strVal.length; nPos++) { if (whitespace.indexOf(strVal.charAt(nPos)) == -1) break; } return strVal.substring(nPos); } // alltrim: Trim the string on left and right, and return the trimmed value function alltrim(strVal) { return (isEmpty(strVal) ? strVal:ltrim(rtrim(strVal))); } /******************************************************** replace: useful for JavaScript 1.0 and 1.1 where replace is not available. With JavaScript 1.2 (and above) replace function is available. : replaces one occurrence of strVal with strWith in strSrc ********************************************************/ function replace(strSrc, strVal, strWith) { var nPos = 0, strLeft="", strRight=""; // check if empty (or) no string is found to replace if (isEmpty(strSrc) || (strSrc.indexOf(strVal) < 0)) return strSrc; nPos = strSrc.indexOf(strVal); strLeft = strSrc.substring(0, nPos); nPos += strVal.length; strRight = strSrc.substring(nPos); return (strLeft + strWith + strRight); } // replaceall : replace all occurrences of strVal with strWith in strSrc function replaceall(strSrc, strVal, strWith) { var strBuffer=strSrc; while (strBuffer.indexOf(strVal) >= 0) strBuffer = replace(strBuffer, strVal, strWith); return (strBuffer); } // occurs: return no of occurrences of strVal within strSrc function occurs(strVal, strSrc) { var nCnt = 0, strBuffer=strSrc; while (strBuffer.indexOf(strVal) >= 0) { strBuffer = replace(strBuffer, strVal, ""); nCnt++; } return (nCnt); } // replicate: returns string that contains strVal repeated nCnt times function replicate(strVal, nCnt) { var i = 0, strBuffer = ""; for (i = 0; i < nCnt; i++) strBuffer += strVal; return (strBuffer); } /************************************************************** stuff: returns string after replacing nCnt characters (starting from nStartPos) with strReplacement in strSrc. NOTE: starting position is 0 **************************************************************/ function stuff(strSrc, nStartPos, nCnt, strReplacement) { var strLeft= "", strRight=""; // check boundary values if (nCnt < 0 || nStartPos < 0 || (nStartPos > strSrc.length-1)) return strSrc; strLeft = strSrc.substring(0, nStartPos); strRight = strSrc.substring(nStartPos+nCnt); return (strLeft + strReplacement + strRight); } // whitespace characters: ' ', '\t', '\r', '\n' var whitespace = " \t\n\r"; // isEmpty: Check whether string strVal is empty function isEmpty(strVal) { return ((strVal == null) || (strVal.length == 0)); } // isWhitespace: Check if string strVal has only the whitespace characters function isWhitespace(strVal) { var nPos = 0; if (isEmpty(strVal)) return false; for (nPos = 0; nPos < strVal.length; nPos++) if (whitespace.indexOf(strVal.charAt(nPos)) == -1) return false; return true; } // rtrim: Right trim the string and return the trimmed value function rtrim(strVal) { var nPos = 0; if (isEmpty(strVal)) return strVal; for (nPos = strVal.length-1; nPos >= 0; nPos--) { if (whitespace.indexOf(strVal.charAt(nPos)) == -1) break; } return (nPos == strVal.length-1 ? strVal.substring(0) : strVal.substring(0, nPos+1)); } // ltrim: Left trim the string and return the trimmed value function ltrim(strVal) { var nPos = 0; if (isEmpty(strVal)) return strVal; for (nPos = 0; nPos < strVal.length; nPos++) { if (whitespace.indexOf(strVal.charAt(nPos)) == -1) break; } return strVal.substring(nPos); } // alltrim: Trim the string on left and right, and return the trimmed value function alltrim(strVal) { return (isEmpty(strVal) ? strVal:ltrim(rtrim(strVal))); } /******************************************************** replace: useful for JavaScript 1.0 and 1.1 where replace is not available. With JavaScript 1.2 (and above) replace function is available. : replaces one occurrence of strVal with strWith in strSrc ********************************************************/ function replace(strSrc, strVal, strWith) { var nPos = 0, strLeft="", strRight=""; // check if empty (or) no string is found to replace if (isEmpty(strSrc) || (strSrc.indexOf(strVal) < 0)) return strSrc; nPos = strSrc.indexOf(strVal); strLeft = strSrc.substring(0, nPos); nPos += strVal.length; strRight = strSrc.substring(nPos); return (strLeft + strWith + strRight); } // replaceall : replace all occurrences of strVal with strWith in strSrc function replaceall(strSrc, strVal, strWith) { var strBuffer=strSrc; while (strBuffer.indexOf(strVal) >= 0) strBuffer = replace(strBuffer, strVal, strWith); return (strBuffer); } // occurs: return no of occurrences of strVal within strSrc function occurs(strVal, strSrc) { var nCnt = 0, strBuffer=strSrc; while (strBuffer.indexOf(strVal) >= 0) { strBuffer = replace(strBuffer, strVal, ""); nCnt++; } return (nCnt); } // replicate: returns string that contains strVal repeated nCnt times function replicate(strVal, nCnt) { var i = 0, strBuffer = ""; for (i = 0; i < nCnt; i++) strBuffer += strVal; return (strBuffer); } /************************************************************** stuff: returns string after replacing nCnt characters (starting from nStartPos) with strReplacement in strSrc. NOTE: starting position is 0 **************************************************************/ function stuff(strSrc, nStartPos, nCnt, strReplacement) { var strLeft= "", strRight=""; // check boundary values if (nCnt < 0 || nStartPos < 0 || (nStartPos > strSrc.length-1)) return strSrc; strLeft = strSrc.substring(0, nStartPos); strRight = strSrc.substring(nStartPos+nCnt); return (strLeft + strReplacement + strRight); } // isDigit: check if val has digits (0-9) function isDigit(val) { var strBuffer = new String(val); var nPos = 0; if (isEmpty(strBuffer)) return false; for (nPos = 0; nPos < strBuffer.length; nPos++) if (strBuffer.charAt(nPos) < '0' || strBuffer.charAt(nPos) > '9') return false; return true; } // isAlpha: check if val has alphabets only (a-z, A-Z) function isAlpha(val) { var strBuffer = new String(val); var nPos = 0; if (isEmpty(strBuffer)) return false; for (nPos = 0; nPos < strBuffer.length; nPos++) if (!((strBuffer.charAt(nPos) >= 'a' && strBuffer.charAt(nPos) <= 'z') || (strBuffer.charAt(nPos) >= 'A' && strBuffer.charAt(nPos) <= 'Z'))) return false; return true; } // isInteger: check if nVal is integer type function isInteger(nVal) { var strBuffer = new String(nVal); var nPos = 0, nStart = 0; if (isEmpty(strBuffer)) return true; if (isWhitespace(strBuffer)) return false; // check if -ve or +ve sign occurs in the beginning if ((strBuffer.charAt(0) == '-') || (strBuffer.charAt(0) == '+')) nStart = 1; else nStart = 0; for (nPos = nStart; nPos < strBuffer.length; nPos++) if (strBuffer.charAt(nPos) < '0' || strBuffer.charAt(nPos) > '9') return false; return true; } // isFloat: check if fVal is floating-point type // LIMITATION: no scientific notation (for eg: xxxx.xxE+xx) function isFloat(fVal) { var strBuffer = new String(fVal); var nPos = 0, nStart = 0; if (isEmpty(strBuffer)) return true; if (isWhitespace(strBuffer)) return false; // check if -ve or +ve sign occurs in the beginning if ((strBuffer.charAt(0) == '-') || (strBuffer.charAt(0) == '+')) nStart = 1; else nStart = 0; for (nPos = nStart; nPos < strBuffer.length; nPos++) if ((strBuffer.charAt(nPos) < '0' || strBuffer.charAt(nPos) > '9') && (strBuffer.charAt(nPos) != '.')) return false; return true; } /************************************************************ isDate : Check if dVal is valid date : valid date format - MM (or) M / DD (or) D / Y (or) YY (or) YYYY : YY > 50 should be 19YY : YY <= 50 should be 20YY *************************************************************/ function isDate(dVal) { var strBuffer= new String(dVal); var cDelimiter=''; var strMonth=0, strDay=0, strYear=0; var nPos=-1; if (isEmpty(strBuffer)) return true; if (isWhitespace(strBuffer)) return false; // Get the delimiter used if (occurs('/', strBuffer) == 2) cDelimiter = '/'; else if (occurs('-', strBuffer) == 2) cDelimiter = '-'; // If no '/' or '-' found return false if (cDelimiter == '') return false; // validate month, date, and year (Y, YY, YYYY are valid year formats) nPos = strBuffer.indexOf(cDelimiter); strMonth = strBuffer.substring(0, nPos); if (strMonth.length > 2 || !isDigit(strMonth)) return false; strBuffer = strBuffer.substring(nPos+1); nPos = strBuffer.indexOf(cDelimiter); strDay = strBuffer.substring(0, nPos); if (strDay.length > 2 || !isDigit(strDay)) return false; strBuffer = strBuffer.substring(nPos+1); strYear = strBuffer; if ((strYear.length > 4) || (strYear.length == 3) || !isDigit(strYear)) return false; // if YY < 50 then YYYY=20YY, else if YY >= 50 then YYYY=19YY var iYear = parseInt(strYear); if (iYear < 50) strYear = "20" + (strYear < 10 ? '0' + strYear:strYear); else if (iYear >= 50 && iYear < 100) strYear = "19" + strYear; strBuffer = strMonth + cDelimiter + strDay + cDelimiter + strYear; // validate date var dBuffer = new Date(strBuffer); if (dBuffer.getDate() != parseInt(strDay) || dBuffer.getMonth()+1 != parseInt(strMonth) || dBuffer.getFullYear() != parseInt(strYear)) return false; return true; } // isZipcode: check if valid US zip code (##### or #####-####) function isZipcode(strZip) { var strLeft="", strRight="", strVal = new String(strZip); if (isEmpty(strVal)) return true; if (isWhitespace(strVal)) return false; if (strVal.length != 5 && strVal.length != 10) return false; if ((strVal.length == 5) && isDigit(strVal)) return true; if ((strVal.length == 10) && isDigit(strVal.substring(0, 5)) && isDigit(strVal.substring(6))) return true; return false; } // isDatePart: check if nVal is valid day: 1-31, month:1-12, year:YYYY function isDatePart(nVal, strType) { var strBuffer = new String(nVal); if (isEmpty(strBuffer) || isWhitespace(strBuffer)) return false; nVal = parseInt(strBuffer); if (!isDigit(nVal)) return false; if ((strType == "Year") && (nVal < 0 || (nVal > 99 && nVal < 1000) || nVal > 9999)) return false; else if ((strType == "Month") && (nVal < 1 || nVal > 12)) return false; else if ((strType == "Day") && (nVal < 1 || nVal > 31)) return false; return true; } /********************************************************************** Depends on: alltrim(), isEmpty(), replaceall() isFloat(), isDigit(), isZipcode() NOTE: -- always alltrim field value before calling any of these functions -- if field is empty, the function call will return true **********************************************************************/ function checkField(objFld, strType) { objFld.value = alltrim(objFld.value); if (isEmpty(objFld.value)) return true; // Remove commas from Integer and Float fields if ((strType == "Integer") || (strType == "Float")) objFld.value = replaceall(objFld.value, ",", ""); if ((strType == "Digit") && !isDigit(objFld.value)) { alert("Please enter a response in numerical format (please " + "exclude dollar signs, commas, parenthesis or any other " + "non-numeric symbols)."); objFld.value = ""; objFld.focus(); return false; } if (strType == "Integer") if (!isInteger(parseInt(objFld.value))) { alert("Please enter a response in numerical format (please " + "exclude dollar signs, commas, parenthesis or any other " + "non-numeric symbols)."); objFld.value = ""; objFld.focus(); return false; } else objFld.value = parseInt(objFld.value); if (strType == "Float") if (!isFloat(parseFloat(objFld.value))) { alert("Please enter a response in numerical format (please " + "exclude dollar signs, commas, parenthesis or any other " + "non-numeric symbols)."); objFld.value = ""; objFld.focus(); return false; } else objFld.value = parseFloat(objFld.value); if (strType == "ZIP") if (!isZipcode(objFld.value)) { alert("Please enter valid zip code value"); objFld.value = ""; objFld.focus(); return false; } if (strType == "Date") if (!isDate(objFld.value)) { alert("Please enter valid date value (MM/DD/YYYY)"); objFld.value = ""; objFld.focus(); return false; } if (strType == "DatePartYear" && checkField(objFld, "Digit")) { if (!isDatePart(objFld.value, "Year")) { alert("Please enter valid year value"); objFld.value = ""; objFld.focus(); return false; } else if (objFld.value < 100) objFld.value = (objFld.value < 50 ? 2000:1900) + parseInt(objFld.value); } return true; }