// isDecimalNumber function isDecimalNumber (passedVal) { if(passedVal == "") { return false; } for (z=0; z "9") { return false; } } return true; } // isEmpty function isEmpty (strValue) { return (! strValue.replace (/^(\s*)/, "", strValue)); } // isInteger function isInteger (passedVal) { if(passedVal == "") { return false; } for (z=0; z "9") { return false; } } return true; } // isRadioSelected function isRadioSelected (radioObject) { eventOption = -1; if (radioObject[0]) { for ( z=0; z < radioObject.length; z++) { if (radioObject[z].checked) { eventOption = z; } } } else { if (radioObject.checked) { eventOption = 1; } } if(eventOption == -1) { return false; } else { return true; } } // isSelected function isSelected (strValue) { if (strValue == "0" || strValue == "") return false; else return true; } // Return "true" if "monthnum" and "yearnum" constitute a valid credit card date. // // Created (16 Oct 2000) Raymond Woo // Modified (24 May 2001) Raymond Woo -- optimised code for checking : 2 digits only for month/year each function isValidCCDate(monthnum, yearnum) { var now = new Date(); var futureexpiry = (now.getYear() + 10); // within x years // Must be 2 digits each if ((! /^\d{2}$/.test(monthnum)) || (! /^\d{2}$/.test(yearnum))) return false; // Cannot have expired and must be within x years. if (monthnum > 12 || monthnum < 1) return false; if (yearnum > (futureexpiry%100) || yearnum < (now.getYear()%100)) return false; if (yearnum == (now.getYear()%100) && monthnum <= now.getMonth()) return false; return true; } // Return "true" if "numbertocheck" is a valid credit card number, as defined by the Luhn algorithm. // // Created (16 Oct 2000) Raymond Woo // Modified (24 May 2001) Raymond Woo -- optimised code for checking : 16-19 digits only // Modified (20 Aug 2001) Raymond Woo -- doh, AMEX is 15 digits. have now dropped the lowerbound to 5, in case of proprietry cards function isValidCCNumber(numbertocheck) { var weight = 1; var checktotal = 0; //Characters can only be (5 to 19) digits. if (! /^\d{5,19}$/.test(numbertocheck)) return false; //LUHN check. if (numbertocheck.length % 2 == 0) {weight = 2} else {weight = 1} for (z=0; z <= numbertocheck.length-1; z++) { digit = numbertocheck.substring(z, z+1); val = digit * weight; if (val > 9) {val = val - 9} checktotal = checktotal + val; weight = (weight == 2) ? 1 : 2; } if (checktotal %10 == 0) {return true} else {return false} } // Returns an error message if there is an error with the credit card, returns null otherwise. // This requires the isValidCCNumber and isValidCCDate javascript functions. // Input: numbertocheck = the credit card number - digits ONLY, no spaces // monthnum, yearnum = the expiry month and year (2-digit year) // cardtype = one of "VISA", "MAST","BANK", "DINE", "AMEX", "JCB", "PROP" // // Created (24 May 2001) Raymond Woo function validateCCard (numbertocheck, monthnum, yearnum, cardtype) { if (!isValidCCNumber(numbertocheck)) return "Credit Card Number is not valid"; if (!isValidCCDate(monthnum, yearnum)) return "Expiry Date is not valid"; switch (cardtype) { case "VISA" : if (! /4\d+/.test(numbertocheck)) return "Card number does not appear to be valid for a VISA"; break; case "MAST" : if (! /5[12345]\d+/.test(numbertocheck)) return "Card number does not appear to be valid for a Mastercard"; break; case "BANK" : if ((! /56022[12345]\d+/.test(numbertocheck)) && (! /56105\d+/.test(numbertocheck))) return "Card number does not appear to be valid for a Bankcard"; break; case "DINE" : if (! /3[0689]\d+/.test(numbertocheck)) return "Card number does not appear to be valid for a Diners Club"; break; case "AMEX" : if (! /3[47]\d+/.test(numbertocheck)) return "Card number does not appear to be valid for an American Express"; break; case "JCB" : if ((! /1800\d+/.test(numbertocheck)) && (! /2131\d*/.test(numbertocheck)) && (! /35(28|29|[345678])\d+/.test(numbertocheck))) return "Card number does not appear to be valid for a JCB"; break; case "PROP" : if (! /5[078]\d+/.test(numbertocheck)) return "Card number does not appear to be valid for a Proprietry Card"; break; default : return "Card Type is not valid"; } return null; // validated } // Checks the date input for a valid format. // This code accepts the date formats: DD/MM/YYYY or DD-MM-YYYY // To alter the format checked for, change the datePat (date pattern) - // d{2} requires 2 digits, d{1,2} requires either 1 or 2 digits etc. // \/|- requires / or - as a separator etc. // // Created (16 Oct 2000) Raymond Woo function isValidDate (strValue) { var datePat = /^(\d{2})(\/)(\d{2})\2(\d{4})$/; // Change this to check for a different format var matchArray = strValue.match(datePat); // is the format ok? if (matchArray == null) { return false; } day = matchArray[1]; month = matchArray[3]; year = matchArray[4]; if (month < 1 || month > 12) { return false; } if (day < 1 || day > 31) { return false; } if ((month==4 || month==6 || month==9 || month==11) && day==31) { return false; } if (month==2) { var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); if (day>29 || (day==29 && !isleap)) { return false; } } return true; // date is valid } // isValidEmail function isValidEmail (emailStr) { var emailPat=/^(.+)@(.+)$/; var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"; var validChars="\[^\\s" + specialChars + "\]"; var quotedUser="(\"[^\"]*\")"; var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/; var atom=validChars + '+'; var word="(" + atom + "|" + quotedUser + ")"; var userPat=new RegExp("^" + word + "(\\." + word + ")*$"); var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$"); var matchArray=emailStr.match(emailPat) if (matchArray==null) { return false; } var user=matchArray[1]; var domain=matchArray[2]; if (user.match(userPat)==null) { return false; } var IPArray=domain.match(ipDomainPat) if (IPArray!=null) { for (var i=1;i<=4;i++) { if (IPArray[i]>255) { return false; } } return true; } var domainArray=domain.match(domainPat) if (domainArray==null) { return false; } var atomPat=new RegExp(atom,"g") var domArr=domain.match(atomPat) var len=domArr.length if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) { return false; } if (len<2) { return false; } return true; } // Returns "true" if the "strValue" is in a valid 12 hour time format, eg. HH:MM or H:MM // P or PM or A or AM are optional at the end, and are case insensitive. // // Created (16 Oct 2000) Raymond Woo // Modified (27 Nov 2000) Raymond Woo "AM" matching corrected. Doh. /* function isValidTime ( strValue ) { var objRegExp = /^(0{0,1}[1-9]|1[0-2]):[0-5]\d\s*([A]|[P])M*$/i; return objRegExp.test( strValue ); } */ // isValidURL function isValidURL (strValue) { // CHECK IF HAS VALID URL PREFIX var urlPrefixArray = new Array(); urlPrefixArray[0] = "file"; urlPrefixArray[1] = "ftp"; urlPrefixArray[2] = "gopher"; urlPrefixArray[3] = "http"; urlPrefixArray[4] = "https"; urlPrefixArray[5] = "telnet"; urlPrefixArray[6] = "wais"; urlPrefixArray[7] = "mailto"; urlPrefixArray[8] = "news"; hasPrefix = false; stringAddress = ""; var stringArray = strValue.split(":"); stringPrefix = stringArray[0]; if (stringArray.length >= 2) stringAddress = stringArray[1]; var addressArray = stringAddress.split("//"); if (addressArray.length >= 2) stringAddress = addressArray[1]; for (i = 0; i < urlPrefixArray.length; i++) { if(urlPrefixArray[i] == stringPrefix) { hasPrefix = true; } } // IF NO VALID PREFIX - RETURN FALSE if (!hasPrefix) return false; // are regular expressions supported? var supported = 0; if (window.RegExp) { var tempStr = "a"; var tempReg = new RegExp(tempStr); if (tempReg.test(tempStr)) supported = 1; } // check if right format if (supported) { var r1 = new RegExp(/^[a-z0-9\-]+(\.[a-z0-9\-]+)*(\.[a-z]{2,3})+$/); return (r1.test(stringAddress)); } else { return true; } } // Eliminates everything apart from digits (0 through 9) from a string. // // Created (16 Oct 2000) Raymond Woo function reduceToDigits (strValue) { return (strValue.replace (/([^0-9])/g, "", strValue)); } // Compare two dates (dd/mm/yyyy) // return 0 if 'end' is greater than 'start' function compareTwoDate(start, end) { // start date comparison var startDateArray = start.split("/"); var startDate = new Date; startDate.setDate(startDateArray[0]); startDate.setMonth(startDateArray[1]-1); startDate.setFullYear(startDateArray[2]); var endDateArray = end.split("/"); var endDate = new Date; endDate.setDate(endDateArray[0]); endDate.setMonth(endDateArray[1]-1); endDate.setFullYear(endDateArray[2]); if (endDate < startDate) { return 1; } else { return 0; } } // isValidTime function isValidTime (strTime) { return (strTime.length == 4) && (strTime <= 2400) && (strTime >= 0000); } // reduceToDigits function reduceToDigits (strValue) { return (strValue.replace (/([^0-9])/g, "", strValue)); } // isValidAcnAbnNumber function isValidAcnAbnNumber(numberToCheck) { numberToCheck = reduceToDigits(numberToCheck); if ( (numberToCheck.length != 11) && (numberToCheck.length != 14) ) { return false; } else { return true; } /* numberToCheck = reduceToDigits(numberToCheck); if (numberToCheck.length == 9) { weight = 0; checkTotal = 0; checkDigit = numberToCheck.substring(numberToCheck.length - 1, numberToCheck.length); for (i = (numberToCheck.length - 1); i >= 0; i--) { digit = numberToCheck.substring(i, i+1); checkTotal = checkTotal + (digit * weight); weight++; } remainder = checkTotal % 10; if (remainder == 0) { caluclatedCheckDigit = 0; } else { caluclatedCheckDigit = 10 - remainder; } if (caluclatedCheckDigit == checkDigit) { return true; } else { return false; } } else if (numberToCheck.length == 11) { weightArray = new Array(10,1,3,5,7,9,11,13,15,17,19); checkTotal = 0; firstNumber = numberToCheck.substring(0, 1); firstNumber = firstNumber - 1; newNumber = firstNumber + numberToCheck.substring(1, numberToCheck.length); for (i = 0; i <= newNumber.length - 1 ; i++) { digit = newNumber.substring(i, i+1); checkTotal = checkTotal + (digit * weightArray[i]); } remainder = checkTotal % 89; if (remainder == 0) { return true; } else { return false; } } else { return false; } */ } ////////////////////////////////////////////////////////////////////////////////////// ////// ////// ////// FORM CHECKING ////// ////// ////// ////////////////////////////////////////////////////////////////////////////////////// //checkUpdateContents function checkLoginForm(formObj) { var alert_message = ""; if (isEmpty(formObj.usrLogin.value)) { alert_message += " User Login.\n"; } if (isEmpty(formObj.usrPassword.value)) { alert_message += " User Password.\n"; } if (alert_message) { alert ("Your form is incomplete.\n" + "You must supply the following information:\n" + alert_message ) return false; } else { return true; } } //check to date is sfter from date on Calendar maintenance forms function checkCalendarDate(formObj) { if (!isEmpty(formObj.calendarDateTo.value)) { datefrom=formObj.calendarDateFrom.value; dateto=formObj.calendarDateTo.value; var datefrom1 = datefrom.substr(3,2) +"/" + datefrom.substr(0,2)+"/" + datefrom.substr(6,4); var dateto1 = dateto.substr(3,2) +"/" + dateto.substr(0,2)+"/" + dateto.substr(6,4); datefrom2 = new Date(datefrom1); dateto2 = new Date(dateto1); if(dateto2 < datefrom2) { alert ("Please supply a valid To date.\n"); return false; } else { return true; } } else { return true; } } //checkFormComplete function checkFormComplete(formObj) { var alert_message = ""; if (formObj.inputFields.value == undefined) { var inpFieldsArray = formObj.inputFields; } else { var inpFieldsArray = new Array(formObj.inputFields); } for ( var i = 0; i < inpFieldsArray.length ; i++ ) { var fieldName = inpFieldsArray[i].value.split('|'); var inputName = fieldName[0]; var fieldIsRequired = fieldName[1]; var fieldTitle = fieldName[2]; var fieldValidationType = fieldName[3]; if (fieldIsRequired == "required") { alert_message += runValidation( formObj, inputName, fieldTitle, fieldValidationType ); } else if (fieldIsRequired == "optional") { if (eval("!isEmpty(formObj." + inputName + ".value)")) { alert_message += runValidation( formObj, inputName, fieldTitle, fieldValidationType ); } } } if (alert_message) { alert ("Your form is incomplete.\n" + "You must supply the following information:\n" + alert_message ) return false; } else { return true; } } function runValidation( formObj, inputName, fieldTitle, fieldValidationType ) { alert_message = ""; switch (fieldValidationType) { case 'list': alert_message += checkListComplete(formObj,inputName, fieldTitle); break; case 'text': alert_message += checkTextBoxComplete(formObj,inputName, fieldTitle); break; case 'int': alert_message += checkIntegerComplete(formObj,inputName, fieldTitle); break; case 'decimal': alert_message += checkDecimalComplete(formObj,inputName, fieldTitle); break; case 'textarea': alert_message += checkTextAreaComplete(formObj,inputName, fieldTitle); break; case 'email': alert_message += checkEmailComplete(formObj,inputName, fieldTitle); break; case 'url': alert_message += checkUrlComplete(formObj,inputName, fieldTitle); break; case 'date': alert_message += checkDateComplete(formObj,inputName, fieldTitle); break; case 'ccnumber': alert_message += checkCreditCardNumberComplete(formObj,inputName, fieldTitle); break; case 'ccdate': alert_message += checkCreditCardDateComplete(formObj,inputName, fieldTitle); break; case 'radio': alert_message += checkRadioComplete(formObj,inputName, fieldTitle); break; } return alert_message; } function checkRadioComplete(formObj, inputName, fieldTitle) { var alert_message = ""; if (eval("!isRadioSelected(formObj." + inputName + ")")) { alert_message = alert_message + " " + fieldTitle + "\n"; } return alert_message; } function checkListComplete(formObj, inputName, fieldTitle) { var alert_message = ""; if (eval("!isSelected(formObj." + inputName + ".value)")) { alert_message = alert_message + " " + fieldTitle + "\n"; } return alert_message; } function checkCreditCardNumberComplete(formObj, inputName, fieldTitle) { var alert_message = ""; if (eval("!isValidCCNumber(formObj." + inputName + ".value)")) { alert_message = alert_message + " " + fieldTitle + "\n"; } return alert_message; } function checkCreditCardDateComplete(formObj, inputName, fieldTitle) { var alert_message = ""; var tempDate = eval('formObj.' + inputName + '.value'); var dateArray = tempDate.split('/'); if ( dateArray.length == 2 ) { if (!isValidCCDate(dateArray[0], dateArray[1])) { alert_message = alert_message + " " + fieldTitle + "\n"; } } else { alert_message = alert_message + " " + fieldTitle + "\n"; } return alert_message; } function checkIntegerComplete(formObj, inputName, fieldTitle) { var alert_message = ""; if (eval("!isInteger(formObj." + inputName + ".value)")) { alert_message = alert_message + " " + fieldTitle + "\n"; } return alert_message; } function checkUrlComplete(formObj, inputName, fieldTitle) { var alert_message = ""; if (eval("!isValidURL(formObj." + inputName + ".value)")) { alert_message = alert_message + " " + fieldTitle + "\n"; } return alert_message; } function checkDecimalComplete(formObj, inputName, fieldTitle) { var alert_message = ""; if (eval("!isDecimalNumber(formObj." + inputName + ".value)")) { alert_message = alert_message + " " + fieldTitle + "\n"; } return alert_message; } function checkTextBoxComplete(formObj, inputName, fieldTitle) { var alert_message = ""; if (eval("isEmpty(formObj." + inputName + ".value)")) { alert_message = alert_message + " " + fieldTitle + "\n"; } return alert_message; } function checkDateComplete(formObj, inputName, fieldTitle) { var alert_message = ""; if (eval("!isValidDate(formObj." + inputName + ".value)")) { alert_message = alert_message + " Valid " + fieldTitle + "\n"; } return alert_message; } function checkEmailComplete(formObj, inputName, fieldTitle) { var alert_message = ""; if (eval("!isValidEmail(formObj." + inputName + ".value)")) { alert_message = alert_message + " Valid " + fieldTitle + "\n"; } return alert_message; } function checkTextAreaComplete(formObj, inputName, fieldTitle) { var alert_message = ""; if (eval("isEmpty(formObj." + inputName + ".value)")) { alert_message = alert_message + " " + fieldTitle + "\n"; } return alert_message; }