// modified by Simon Lau Kian Lye 12 Mar 2001 // Functions available : // ---------------------------------------------------------------------- // -----> validateMultiEmails // semi-colon separated // -----> validateNRIC(myfield,name) // -----> validateCfrmPswd(myfield,myfield1) // -----> compareDates(date1,date2) // equal=0 date1date2=1 // -----> validateMulipleSel(myfield,name) // -----> validateImgFile(myfield,name) // -----> validateUserName(myfield,name,min,max) 24 Jul 2000 // -----> validateRadio_Check(myfield,name) 19 Jul 2000 // -----> validateDropDown(myfield,name) 19 Jul 2000 // -----> textareaMax(myfield) 19 Jul 2000 // -----> RemoveBad(InStr) // -----> validateString(myfield,name) // -----> validateEmail(myfield,name) // -----> validateNum(myfield,name) // -----> validateFloat(myfield,name) // -----> validateDate(myfield,name) // -----> validateRange1_99(myfield,name) // -----> validateRange1_9999(myfield,name) // -----> validateRange0_99(myfield,name) // -----> validateMoney(myfield,name) // ---------------------------------------------------------------------- function validateMultiEmails(myfield,name) { var id=myfield.value if (!(notNull(id)&& notBlank(id))) { return (name+"\n"); } else { id = id.split(";"); for (var i = 0; i < id.length; i++) { varTemp = id[i]; //alert(varTemp); if (!(isEmail(varTemp))) return (name+"\n"); } } return ""; } //checking for Singaporean NRIC,FIN input function validateNRIC(myfield,name){ var passport_ref = new Array ("","A","B","C","D","E","F","G","H","I","Z","J"); var FIN_passport_ref = new Array ("","K","L","M","N","P","Q","R","T","U","W","X"); //myfield.value.toUpperCase(); passportnum = myfield.value.toUpperCase(); if (passportnum.substring(0,1)!='T' && passportnum.substring(0,1)!='G' && passportnum.substring(0,1)!='S') passportnum = 'S' + passportnum; total= passportnum.substring(1,2) * 2 + passportnum.substring(2,3) * 7 + passportnum.substring(3,4) * 6 + passportnum.substring(4,5) * 5 + passportnum.substring(5,6) * 4 + passportnum.substring(6,7) * 3 + passportnum.substring(7,8) * 2 ; if (passportnum.substring(0,1)=='T' || passportnum.substring(0,1)=='G') total = total + 4; if (passportnum.substring(0,1)=='S' || passportnum.substring(0,1)=='T') checkdigit = passport_ref[11 - total%11] ; else checkdigit = FIN_passport_ref[11 - total%11] ; if (passportnum.substring(8,9)==checkdigit) return ""; return (name+"\n"); } Date.prototype.getDateString = getDateString; Date.prototype.getFullYear = getFullYear; function validateCfrmPswd(myfield,myfield1) { if ((myfield.value=='')||(myfield1.value=='')) return ""; else { if (myfield.value==myfield1.value) return ""; else return ('\n* Confirmation password differ from the new password\n'); } } function compareDates(date1,date2){ if ((isDate(date1.value)) && (isDate(date2.value))) { vardate1 = new Date(date1.value); vardate2 = new Date(date2.value); if(vardate1.getDateString() == vardate2.getDateString()) return(0); else if(vardate1.getDateString() < vardate2.getDateString()) return(-1); else return(1); } else return (2); } function compareDates2(date1,date2){ if ((isDate(date1)) && (isDate(date2))) { vardate1 = new Date(date1); vardate2 = new Date(date2); if(vardate1.getDateString() == vardate2.getDateString()) return(0); else if(vardate1.getDateString() < vardate2.getDateString()) return(-1); else return(1); } else return (2); } function getDateString(){ var dateStr; dateStr = "" + this.getFullYear(); if (this.getMonth() < 9) dateStr += "0"; dateStr += (this.getMonth() + 1); if (this.getDate() < 10) dateStr += "0"; dateStr += this.getDate(); return dateStr; } function getFullYear(){ var year = this.getYear(); if(year < 1000){ year += 1900;} return year } function validateMulipleSel(myfield,name) { count=myfield.length; allValid=false; for (ic=0;icmax) allvalid = false; } var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"; var checkStr = myfield.value; for (i = 0; i < checkStr.length; i++) { ch = checkStr.charAt(i); for (j = 0; j < checkOK.length; j++) if (ch == checkOK.charAt(j)) break; if (j == checkOK.length) { allValid = false; break; } } var checkNotOK = "1234567890"; var ch = checkStr.charAt(0); for (i=0; i < checkNotOK.length; i++) { if (ch == checkNotOK.charAt(i)) { allValid = false; break; } } if (!allValid) return (name+"\n"); else return ""; } function validateMulipleSel(myfield,name) { count=myfield.length; allValid=false; for (ic=0;ic0) { for (ic=0;ic maxlimit) // if too long...trim it! myfield.value = myfield.value.substring(0, maxlimit); } // eg. function RemoveBad(InStr){ InStr = InStr.replace(/\/g,""); InStr = InStr.replace(/\"/g,""); InStr = InStr.replace(/\'/g,""); InStr = InStr.replace(/\%/g,""); InStr = InStr.replace(/\;/g,""); InStr = InStr.replace(/\(/g,""); InStr = InStr.replace(/\)/g,""); InStr = InStr.replace(/\&/g,""); InStr = InStr.replace(/\+/g,""); return InStr; } function validateString(myfield,name) { if (notNull(myfield.value)&& notBlank(myfield.value)) return ""; else return (name+"\n"); } function validateEmail(myfield,name) { if (notNull(myfield.value)&& notBlank(myfield.value) && isEmail(myfield.value)) return ""; else return (name+"\n"); } function validateNum(myfield,name) { //alert("hello"); if (notNull(myfield.value)&& notBlank(myfield.value)&&isDigits(myfield.value)) return ""; else return (name+"\n"); } function validateFloat(myfield,name) { if (notNull(myfield.value)&& notBlank(myfield.value)&&isNumber(myfield.value)) return ""; else return (name+"\n"); } function validateDate(myfield,name) { if (notNull(myfield.value)&& notBlank(myfield.value)&&isDate(myfield.value)) return ""; else return (name+"\n"); } function validateRange1_9999(myfield,name) { if (isDigits(myfield.value) && isInRange(myfield.value,1, 9999)) return ""; else return (name+"\n"); } function validateRange1_99(myfield,name) { if (isDigits(myfield.value) && isInRange(myfield.value,1, 99)) return ""; else return (name+"\n"); } function validateRange0_99(myfield,name) { if (isDigits(myfield.value) && isInRange(myfield.value,0, 99)) return ""; else return (name+"\n"); } function validateMoney(myfield,name) { if (notNull(myfield.value)&& notBlank(myfield.value)) { if(isMoney(myfield.value)) return ""; else return (name+"\n"); } else return (name+"\n"); } // end of Functions //====================================================================== // Begin String Validation //====================================================================== function notNull(str) { if (str.length == 0 ) return false else return true } function notBlank(str) { for (i = 0; i < str.length; i++) { if (str.charAt(i) != " ") return true } return false } function isSize(str, size) { if (str.length == size) return true else return false } //====================================================================== // End String Validation //====================================================================== //====================================================================== // Begin Numeric Validation //====================================================================== function isDigits(str) { var i for (i = 0; i < str.length; i++) { mychar = str.charAt(i) if (mychar < "0" || mychar > "9") return false } return true } function isNumber(str) { numdecs = 0 for (i = 0; i < str.length; i++) { mychar = str.charAt(i) if ((mychar >= "0" && mychar <= "9") || mychar == ".") { if (mychar == ".") numdecs++ } else return false } if (numdecs > 1) return false return true } function isInRange(str, num1, num2) { while (str.charAt(0)=="0") str = str.substring(1); //remove all leading zeros var i = parseInt(str) //parseInt won't work correctly for number with leading zero(s) return((i >= num1) && (i <= num2)) } function isMoney(str) { var tempStr; tempStr = str; tempStr = stripChars(tempStr, '$'); return isNumber(tempStr); } //====================================================================== // End Numeric Validation //====================================================================== //====================================================================== // Begin Formatting //====================================================================== function stripNonDigits(str) { var i var newstring = "" for (i = 0; i < str.length; i++) { mychar = str.charAt(i) if (isDigits(mychar)) newstring += mychar } return newstring } function stripChars(str, chars) { var i var newstring = "" for (i = 0; i < str.length; i++) { mychar = str.charAt(i) if (chars.indexOf(mychar) == -1) newstring += mychar } return newstring } // Removes initial (leading) whitespace characters from s. // Global variable whitespace (see above) // defines which characters are considered whitespace. function stripInitialWhitespace (s) { var i = 0; while ((i < s.length) && charInString (s.charAt(i), whitespace)) i++; return s.substring (i, s.length); } function charInString (c, s) { for (i = 0; i < s.length; i++) { if (s.charAt(i) == c) return true; } return false } //====================================================================== // End Formatting //====================================================================== //====================================================================== // Begin : Email Validation //====================================================================== // isEmail (STRING s [, BOOLEAN emptyOK]) // // Email address must be of form a@b.c -- in other words: // * there must be at least one character before the @ // * there must be at least one character before and after the . // * the characters @ and . are both required // // For explanation of optional argument emptyOK, // see comments of function isInteger. function isEmail (s) { if (isEmpty(s)) if (isEmail.arguments.length == 1) return false; else return (isEmail.arguments[1] == true); // is s whitespace? if (isWhitespace(s)) return false; // there must be >= 1 character before @, so we // start looking at character position 1 // (i.e. second character) var i = 1; var sLength = s.length; // look for @ while ((i < sLength) && (s.charAt(i) != "@")) { i++ } if ((i >= sLength) || (s.charAt(i) != "@")) return false; else i += 2; // look for . while ((i < sLength) && (s.charAt(i) != ".")) { i++ } // there must be at least one character after the . if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false; else return true; } //====================================================================== // End : Email Validation //====================================================================== //====================================================================== // Begin : Whitespace Validation //====================================================================== var whitespace = " \t\n\r"; function isWhitespace (s) { var i; // Is s empty? if (isEmpty(s)) return true; // Search through string's characters one by one // until we find a non-whitespace character. // When we do, return false; if we don't, return true. for (i = 0; i < s.length; i++) { // Check that current character isn't whitespace. var c = s.charAt(i); if (whitespace.indexOf(c) == -1) return false; } // All characters are whitespace. return true; } function isEmpty(s) { return ((s == null) || (s.length == 0)) } //====================================================================== // End : Whitespace Validation //====================================================================== //====================================================================== // Begin : Date Validation //====================================================================== // isYear (STRING s [, BOOLEAN emptyOK]) // // isYear returns true if string s is a valid // Year number. Must be 2 or 4 digits only. // // For Year 2000 compliance, you are advised // to use 4-digit year numbers everywhere. // // And yes, this function is not Year 10000 compliant, but // because I am giving you 8003 years of advance notice, // I don't feel very guilty about this ... // // For B.C. compliance, write your own function. ;-> // // For explanation of optional argument emptyOK, // see comments of function isInteger. function isYear (s) { if (isEmpty(s)) if (isYear.arguments.length == 1) return false; else return (isYear.arguments[1] == true); return ((s.length == 2) || (s.length == 4)); } // isMonth (STRING s [, BOOLEAN emptyOK]) // // isMonth returns true if string s is a valid // month number between 1 and 12. // // For explanation of optional argument emptyOK, // see comments of function isInteger. function isMonth (s) { if (isEmpty(s)) if (isMonth.arguments.length == 1) return false; else return (isMonth.arguments[1] == true); return isInRange (s, 1, 12); } // isDay (STRING s [, BOOLEAN emptyOK]) // // isDay returns true if string s is a valid // day number between 1 and 31. // // For explanation of optional argument emptyOK, // see comments of function isInteger. function isDay (s) { if (isEmpty(s)) if (isDay.arguments.length == 1) return false; else return (isDay.arguments[1] == true); return isInRange (s, 1, 31); } function makeArray(n) { //*** BUG: If I put this line in, I get two error messages: //(1) Window.length can't be set by assignment //(2) daysInMonth has no property indexed by 4 //If I leave it out, the code works fine. // this.length = n; for (var i = 1; i <= n; i++) { this[i] = 0 } return this } var daysInMonth = makeArray(12); daysInMonth[1] = 31; daysInMonth[2] = 29; // must programmatically check this daysInMonth[3] = 31; daysInMonth[4] = 30; daysInMonth[5] = 31; daysInMonth[6] = 30; daysInMonth[7] = 31; daysInMonth[8] = 31; daysInMonth[9] = 30; daysInMonth[10] = 31; daysInMonth[11] = 30; daysInMonth[12] = 31; // daysInFebruary (INTEGER year) // // Given integer argument year, // returns number of days in February of that year. function daysInFebruary (year) { // February has 29 days in any year evenly divisible by four, // EXCEPT for centurial years which are not also divisible by 400. return ( ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 ); } // isDate (STRING year, STRING month, STRING day) // // isDate returns true if string arguments year, month, and day // form a valid date. // function isDateCheck (year, month, day) { // catch invalid years (not 2- or 4-digit) and invalid months and days. if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false; // Explicitly change type to integer to make code work in both // JavaScript 1.1 and JavaScript 1.2. var intYear = parseInt(year); var intMonth = parseInt(month); var intDay = parseInt(day); // catch invalid days, except for February if (intDay > daysInMonth[intMonth]) return false; if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false; return true; } function isDate(DateStr) { todaysdate = new Date(); date = todaysdate.getDate(); day = todaysdate.getDay() + 1; month = todaysdate.getMonth() + 1; yy = todaysdate.getYear(); dateVal = DateStr var firstSlash = dateVal.indexOf("/") var lastSlash = dateVal.lastIndexOf("/") if (firstSlash!=0 && firstSlash == lastSlash) { dateVal = DateStr + "/" + yy; lastSlash = dateVal.lastIndexOf("/") } if (firstSlash != lastSlash) { var month = dateVal.substring(0,firstSlash); var day = dateVal.substring(firstSlash+1,lastSlash); var year = dateVal.substring(lastSlash+1); if (month.substring(0,1) == '0') month = month.substring(1,2); if (day.substring(0,1) == '0') day = day.substring(1,2); if (((day=="")||(month=="")) || (year=="")) return false; else return(isDateCheck(year,month,day)) } else return false; } //====================================================================== // End : Date Validation //======================================================================