/* Script: formValidate.js Author: Subramanya Shastry Y V H - shastry@email.com Modifications: Chris Flynn - chrisflynn@fullcounttech.com Description: Generic form validation routine. Copyright: This script can be freely used with or without modifications. 07/04/01 function: validateForm modified to alert only after all fields have been checked */ //------------------------------------------------------------------------------------ // function: isWhiteSpace // Function to check whether the given argument consists of charactes other // than a space and \t //------------------------------------------------------------------------------------ function isWhiteSpace(argWhiteSpace) { argWs = argWhiteSpace.toString() for (var intI=0; intI < argWs.length; intI++) if (argWs.charAt(intI) != ' ' && argWs.charAt(intI) != '\t') return false return true } //------------------------------------------------------------------------------------ // function: isLeapYear // Function to tell, whether the given year is leap year or not //------------------------------------------------------------------------------------ function isLeapYear(argYear) { return ((argYear % 4 == 0) && (argYear % 100 != 0)) || (argYear % 400 == 0) } //------------------------------------------------------------------------------------ // function: daysInMonth // Function to return the maximum number of days in a given month of a // given year //------------------------------------------------------------------------------------ function daysInMonth(argMonth, argYear) { switch (Number(argMonth)) { case 1: // Jan case 3: // Mar case 5: // May case 7: // Jul case 8: // Aug case 10: // Oct case 12: // Dec return 31; break; case 4: // Apr case 6: // Jun case 9: // Sep case 11: // Nov return 30; break; case 2: // Feb if (isLeapYear(argYear)) return 29 else return 28 break; default: return 0; } } //------------------------------------------------------------------------------------ // function: getDateSeparator // Function to return the date separator // This function expects date in the format of mm/dd/yyyy or mm/dd/yy // or mm-dd-yyyy or mm-dd-yy //------------------------------------------------------------------------------------ function getDateSeparator(argDate) { // Are there invalid separators? if ((argDate.indexOf('-') > 0) && (argDate.indexOf('/') > 0)) return ' ' if (argDate.indexOf('-') > 0) return '-' else if (argDate.indexOf('/') > 0) return '/' else return ' ' } //------------------------------------------------------------------------------------ // function: getYear // Function to return the year part of the given date. // This function expects date in the format of mm/dd/yyyy or mm/dd/yy // or mm-dd-yyyy or mm-dd-yy //------------------------------------------------------------------------------------ function getYear(argDate) { var dateSep = getDateSeparator(argDate) if (dateSep == ' ') return 0 if(argDate.split(dateSep).length == 3) return argDate.split(dateSep)[2] else return 0 } //------------------------------------------------------------------------------------ // function: getMonth // Function to return the month part of the given date. // This function expects date in the format of mm/dd/yyyy or mm/dd/yy // or mm-dd-yyyy or mm-dd-yy //------------------------------------------------------------------------------------ function getMonth(argDate) { var dateSep = getDateSeparator(argDate) if (dateSep == ' ') return 0 if(argDate.split(dateSep).length == 3) return argDate.split(dateSep)[0] else return 0 } //------------------------------------------------------------------------------------ // function: getDay // Function to return the day part of the given date. // This function expects date in the format of mm/dd/yyyy or mm/dd/yy // or mm-dd-yyyy or mm-dd-yy //------------------------------------------------------------------------------------ function getDay(argDate) { var dateSep = getDateSeparator(argDate) if (dateSep == ' ') return 0 if(argDate.split(dateSep).length == 3) return argDate.split(dateSep)[1] else return 0 } //------------------------------------------------------------------------------------ // function: isProperDay // Function to tell whether the given day of the given month is valid //------------------------------------------------------------------------------------ function isProperDay(argDay, argMonth, argYear) { if ((isWhiteSpace(argDay)) || (argDay == 0)) return false if ((argDay > 0) && (argDay < daysInMonth(argMonth, argYear) + 1)) return true else return false } //------------------------------------------------------------------------------------ // function: isProperMonth // Function to tell whether the given month is a valid one //------------------------------------------------------------------------------------ function isProperMonth(argMonth) { if ((isWhiteSpace(argMonth)) || (argMonth == 0)) return false if ((argMonth > 0) && (argMonth < 13)) return true else return false } //------------------------------------------------------------------------------------ // function: isProperYear // Function to tell whether the given Year is a valid one //------------------------------------------------------------------------------------ function isProperYear(argYear) { if ((isWhiteSpace(argYear)) || (argYear.toString().length > 4) || (argYear.toString().length == 3)) return false switch (argYear.toString().length) { case 1: if (argYear >=0 && argYear < 10) return true else return false case 2: if (argYear >=0 && argYear < 100) return true else return false case 4: if (((argYear >=1900) || (argYear >=2000)) && ((argYear < 3000) || (argYear < 2000))) return true else return false default: return false } } //------------------------------------------------------------------------------------ // function: isProperDate // Function to tell whether the given date is valid or not // This function expects date in the format of mm/dd/yyyy or mm/dd/yy // or mm-dd-yyyy or mm-dd-yy //------------------------------------------------------------------------------------ function isProperDate(argDate) { var tmpDay = getDay(argDate) var tmpMon = getMonth(argDate) var tmpYear = getYear(argDate) return isProperDay(tmpDay, tmpMon, tmpYear) && isProperMonth(tmpMon) && isProperYear(tmpYear) } //------------------------------------------------------------------------------------ // function: charOccurences // Function to return the number of times a given character is occuring in // the given string //------------------------------------------------------------------------------------ function charOccurences(argString, argChar) { var intCt = 0 for(var intI=0; intI < argString.length; intI++) if (argString.charAt(intI) == argChar) intCt++ return intCt } //------------------------------------------------------------------------------------ // function: isProperEmail // Function to tell whether the given email is valid or not // modified: 12/17/01 CFF eliminate checks that would fail on valid addresses like first.last@my.domain.com //------------------------------------------------------------------------------------ function isProperEmail(argEmail) { // Check the length... addresses have to be at least 5 chars if (argEmail.length < 5) return false // store the position of @ and . var atPos = argEmail.indexOf('@') var dotPos = argEmail.indexOf('.') // check to see if each char exists if (atPos == -1 || dotPos == -1) return false // @ can't be first or last char if((atPos == 0) || (atPos == (argEmail.length - 1))) return false // . can't be first or last char if((dotPos == 0) || (dotPos == (argEmail.length - 1))) return false return true } //------------------------------------------------------------------------------------ // function: isProperNumber // Function to tell whether the given string is a proper number //------------------------------------------------------------------------------------ function isProperNumber(argNumber) { var numberValue = Number(argNumber) if (isNaN(numberValue)) return false else return !isWhiteSpace(argNumber) } //------------------------------------------------------------------------------------ // function: isProperAlphabetic // Function to tell whether the given string is a proper alphabetic string //------------------------------------------------------------------------------------ function isProperAlphabetic(argString) { var alphabets = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ" for(var intI=0; intI -1) this.required = (argActions.indexOf('[req]') > -1) this.checkDate = (argActions.indexOf('[date]') > -1) this.checkZip = (argActions.indexOf('[zip]') > -1) this.checkNumber = (argActions.indexOf('[number]') > -1) this.checkAlphabetic= (argActions.indexOf('[alpha]') > -1) this.checkUSPhone = (argActions.indexOf('[usphone]') > -1) this.checkUSSSN = (argActions.indexOf('[usssn]') > -1) this.checkAlphanumeric= (argActions.indexOf('[alphanum]') > -1) if (argActions.indexOf('[len=') > -1) { this.checkLength = true var lenToCheck = '' var bolCont = true for (var intI=(argActions.indexOf('[len=') + 5);((intI < argActions.length) && bolCont); intI++) if (argActions.charAt(intI) != ']') lenToCheck += argActions.charAt(intI) else bolCont = false this.lengthToCheck = lenToCheck } else this.checkLength = false if (argActions.indexOf('[blankalert=') > -1) { this.blankAlert = true var alertString = '' var bolCont = true for (var intI=(argActions.indexOf('[blankalert=') + 12);((intI < argActions.length) && bolCont); intI++) if (argActions.charAt(intI) != ']') alertString += argActions.charAt(intI) else bolCont = false this.blankAlertMessage = alertString } else this.blankAlert = false if (argActions.indexOf('[invalidalert=') > -1) { this.invalidAlert = true var alertString = '' var bolCont = true for (var intI=(argActions.indexOf('[invalidalert=') + 14);((intI < argActions.length) && bolCont); intI++) if (argActions.charAt(intI) != ']') alertString += argActions.charAt(intI) else bolCont = false this.invalidAlertMessage = alertString } else this.invalidAlert = false if (argActions.indexOf('[equals=') > -1) { this.shouldEqual = true var equalsString = '' var bolCont = true for (var intI=(argActions.indexOf('[equals=') + 8);((intI < argActions.length) && bolCont); intI++) if (argActions.charAt(intI) != ']') equalsString += argActions.charAt(intI) else bolCont = false this.shouldEqualString = equalsString } else this.shouldEqual = false } //------------------------------------------------------------------------------------ // function: validateForm // Function to validate a HTML form // // 07/04/01 modified to alert only after all fields have been checked //------------------------------------------------------------------------------------ function validateForm(argForm) { var frmElements = argForm.elements var elemName var elemObj var allValid = true var strLineBreak = "\n" var strDash = "- " var strErr = "Please correct the following errors: " + strLineBreak + strLineBreak var strReq = " is a required field and cannot be left blank" for (var intI=0; intI < frmElements.length; intI++) {// * elemObj = frmElements[intI] elemName = elemObj.name if ((elemObj.type == 'hidden') && (elemName.length > 5)) if (elemName.substr(elemName.length - 5).toLowerCase() == '_vldt') {// ** var objAction = new actionFields(objectValue(frmElements, elemName)) var actElem = elemName.substr(0, elemName.length - 5) // Is it a required field? if (objAction.required) { if (isWhiteSpace(objectValue(frmElements, actElem))) {// *** strErr = strErr + strDash + objAction.blankAlertMessage + strReq + strLineBreak allValid = false } // *** } // Check the type validations if ((objectValue(frmElements, actElem) > '') && (!isWhiteSpace(objectValue(frmElements, actElem)))){// *** // Date if (objAction.checkDate) if (!isProperDate(objectValue(frmElements, actElem))) {// **** strErr = strErr + strDash + objAction.invalidAlertMessage + strLineBreak allValid = false } // **** // Number if (objAction.checkNumber) if (!isProperNumber(objectValue(frmElements, actElem))) {// **** strErr = strErr + strDash + objAction.invalidAlertMessage + strLineBreak allValid = false } // **** // Zip if (objAction.checkZip) if (!isProperZip(objectValue(frmElements, actElem))) {// **** strErr = strErr + strDash + objAction.invalidAlertMessage + strLineBreak } // **** // Alphabetic if (objAction.checkAlphabetic) if (!isProperAlphabetic(objectValue(frmElements, actElem))) {// **** strErr = strErr + strDash + objAction.invalidAlertMessage + strLineBreak allValid = false } // **** // Alphanumeric if (objAction.checkAlphanumeric) if (!isProperAlphanumeric(objectValue(frmElements, actElem))) {// **** strErr = strErr + strDash + objAction.invalidAlertMessage + strLineBreak allValid = false } // **** // Check US phone if (objAction.checkUSPhone) if (!isProperUSPhone(objectValue(frmElements, actElem))) {// **** strErr = strErr + strDash + objAction.invalidAlertMessage + strLineBreak allValid = false } // **** // Check US SSN if (objAction.checkUSSSN) if (!isProperUSSSN(objectValue(frmElements, actElem))) {// **** strErr = strErr + strDash + objAction.invalidAlertMessage + strLineBreak allValid = false } // **** // Check email if (objAction.email) if (!isProperEmail(objectValue(frmElements, actElem))) {// **** strErr = strErr + strDash + objAction.invalidAlertMessage + strLineBreak allValid = false } // **** // Check for length if (objAction.checkLength) if (objectValue(frmElements, actElem).length < objAction.lengthToCheck) {// **** strErr = strErr + strDash + objAction.invalidAlertMessage + strLineBreak allValid = false } // **** } // *** } // ** } // * if (allValid) return true else alert (strErr) return false }