/* * * * * * * * * * * * * * * * * * * * VALIDATION HELPERS * * * * * * * * * * * * * * * * * * */ YAHOO.namespace("VAL"); YAHOO.VAL.Validate = function() {}; /** * Checks if a date is valid and within given range * @param {string} date the date in forgiving format * @param {string} range the range (eg. 1-1-1900:1-1-2100) */ YAHOO.VAL.Validate.date = function(date, range) { if(date.length == 0) return -1; var ret = YAHOO.VAL.Validate.isDate(date.toStructuredDate()); if (ret == 0 || isNaN(ret)) { return 1; } else { if(range != undefined && range != "" && range != null) { var range_split = range.split(":"); var current = date.toStructuredDate(); var from = range_split[0].toStructuredDate(); var till = range_split[1].toStructuredDate(); if (YAHOO.VAL.Validate.compareDates(current,from)==1 && YAHOO.VAL.Validate.compareDates(current,till)==-1) { return 0 } else { return 2; } } } return 0; } /** * Validates a date-range * @param {string} fromdate * @param {string} tilldate * @return int (0 = both are equal, -1 fromdate is less then tilldate, -1 fromdate is more then tilldate) */ YAHOO.VAL.Validate.dateRange = function(from,till) { var fromDate = from.toStructuredDate(); var tillDate = till.toStructuredDate(); return YAHOO.VAL.Validate.compareDates(fromDate,tillDate); } /** * Checks if a time is valid * @param {string} time the time in forgiving format */ YAHOO.VAL.Validate.time = function(time,range) { if(time.length == 0) return -1; var ret = YAHOO.VAL.Validate.isTime(time.toStructuredTime()); if (ret == 0 || isNaN(ret)) { return 1; } else { if(range != undefined && range != "" && range != null) { var range_split = range.split(":"); var current = time.toStructuredTime(); var from = range_split[0].toStructuredTime(); var till = range_split[1].toStructuredTime(); if (YAHOO.VAL.Validate.compareDates(current,from,":")==1 && YAHOO.VAL.Validate.compareDates(current,till,":")==-1) return 0 else return 2 } } return 0 } /** * * @param {string} date the date in forgiving format * @param {string} range the range (eg. 1-1-1900:1-1-2100) * @param {string} range the range (eg. 1-1-1900:1-1-2100) */ YAHOO.VAL.Validate.timeRange = function(from,till) { var fromTime = from.toStructuredDate(); var tillTime = till.toStructuredDate(); return YAHOO.VAL.Validate.compareDates(fromTime,tillTime,":"); } /** * Checks if a email address is valid * @param {string} email */ YAHOO.VAL.Validate.email = function(email) { if(email.length == 0) return -1; return (/^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$/).test(email); } /** * Checks if a url is valid * @param {string} url */ YAHOO.VAL.Validate.url = function(url) { if(url.length == 0) return -1; /** * /^(https|http|ftp):\/\/ start with https, http or ftp followed by semicolumn * (.+?) 1 or more greedy characters * (\.([a-z]{2,3})$ must end with dot followed by 2 or 3 chracters (doesn't account for .info, .museum, ...) * /i ignore case **/ return (/^(https|http|ftp):\/\/(.+?)(\.[a-z]{2,3})$/i).test(url); } /** * Checks if a btw/vat is valid * @param {string} countrycode * @param {string} vatcode * @returns 1 if the countrycode and vatcode are valid, 0 if either countrycode or vat code is invalid */ YAHOO.VAL.Validate.vat = function(countryCode, vatCode) { // countrycode or vatcode empty if( vatCode.length == 0 ) return -1; if (countryCode.length == 0 && vatCode.length) { return false; } //remove legal none digit characters: "blank" . - vatCode = vatCode.removeSeperators(); YAHOO.log("remove seperators " + vatCode,"info"); switch(countryCode) { case "BE" : return YAHOO.VAL.Validate.vatBE(vatCode); break; default: return -1; } } YAHOO.VAL.Validate.vatBE = function(vatCode) { // check if is integer if(!YAHOO.VAL.Validate.isInteger(vatCode)) return false; if(vatCode.length == 10) { prefix = vatCode.substring(0,1); vatCode = vatCode.substring(1); if(prefix != "0") return false; } return YAHOO.VAL.Validate.doMod(vatCode.substring(0,7),vatCode.substring(7),97); } YAHOO.VAL.Validate.vatGB = function(vat) { /* For example, a valid number is 232 1462 05. The algorithm only applies to the digits within the number; The last two digits are the check digit, and should be extracted to a separate field. The check digit is calculated as follows: Multiply the first 7 digits in turn by 8,7,6....2 then add up the totals as follows: 2 x 8 = 16 3 x 7 = 21 2 x 6 = 12 1 x 5 = 5 4 x 4 = 16 6 x 3 = 18 2 x 2 = 4 --------- 92 Calculate the modulus 97 of the total. */ }; YAHOO.VAL.Validate.bankaccount = function(countryCode, bankCode) { // countrycode or vatcode empty if( bankCode.length == 0 ) return -1; if (countryCode.length == 0 && bankCode.length) { return false; } //remove legal none digit characters: "blank" . - bankCode = bankCode.removeSeperators(); YAHOO.log("remove seperators " + bankCode,"info"); switch(countryCode) { case "BE" : return YAHOO.VAL.Validate.bankBE(bankCode); break; default: return -1; } }; YAHOO.VAL.Validate.bankBE = function(bankCode) { YAHOO.log("validate belgian bankaccount " + bankCode,"info"); // check if is integer if(!YAHOO.VAL.Validate.isInteger(bankCode)) return false; if(bankCode.length != 12) return false; return YAHOO.VAL.Validate.doMod(bankCode.substring(0,10),bankCode.substring(10),false); } /** * * @param {string} countrycode * @param {string} phonenumber * @returns {boolean} true if valid, false if not valid */ YAHOO.VAL.Validate.phone = function(countryCode , phonenumber) { if (phonenumber.length == 0) return false; phonenumber = phonenumber.replace(/^\+/g, ""); //remove leading + ; phonenumber = phonenumber.replace(/([\\\/\.\-\(\)\s,])/ig, ""); //remove legal none digit characters: "blank", . : - ( ) / \ return (/^[0-9]{3,18}$/).test(phonenumber); //all remaining characters must be digits, between 3 and 18 } /** * * @param {string} INSZ/NISS * @returns boolean (true if the NISS is valid) */ YAHOO.VAL.Validate.niss = function(nissCode) { if (nissCode.length == 0) return -1; //remove legal none digit characters: "blank" . - nissCode = nissCode.removeSeperators(); // check if is integer if(!YAHOO.VAL.Validate.isInteger(nissCode)) return false; // check if code has 11 characters if(nissCode.length != 11) return false; // do mod 97 return YAHOO.VAL.Validate.doMod(nissCode.substring(0,9),nissCode.substring(9),97); } /** * Checks if a date is valid * @param {string} date the date in structured format (31-12-1900) */ YAHOO.VAL.Validate.isDate = function(date) { var date_arr = date.split("-"); var day = date_arr[0]; var month = date_arr[1]; var year = date_arr[2]; // Is month valid ? if (month < 1 || month > 12) return 0; // Is day valid ? if (day < 1 || day > 31) return 0; // Is date valid for month? if (month==2) { // Check for leap year if ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) { // leap year if (day > 29) return 0; } else { if (day > 28) return 0; } } if ((month==4)||(month==6)||(month==9)||(month==11)) { if (day > 30) return 0; } var newdate=new Date(year,month,day); return newdate.getTime(); } /** * Checks if a time is valid * @param {string} time the time in structured format (hh:mm:ss) */ YAHOO.VAL.Validate.isTime = function(time) { var time_arr = time.split(":"); var hour = time_arr[0]; var minute = time_arr[1]; var second = time_arr[2]; // Is month valid ? if (hour < 0 || hour > 23) return 0; if (minute < 0 || minute > 59) return 0; if (second < 0 || second > 59) return 0; var newdate=new Date(1970,1,1,hour,minute,second); return newdate.getTime(); } /** * Compares two dates * @param {string} value1 the first date in structured format (31-12-1900) * @param {string} value2 the second date in structured format * @returns 0 if the dates are the same, -1 if the first one is an earlier date, 1 if the first one is a later date */ YAHOO.VAL.Validate.compareDates = function(value1, value2, sep) { var date1, date2; var month1, month2; var year1, year2; if (!sep) sep = "-"; date1 = value1.substring (0, value1.indexOf ("-")); month1 = value1.substring (value1.indexOf ("-")+1, value1.lastIndexOf ("-")); year1 = value1.substring (value1.lastIndexOf ("-")+1, value1.length); date2 = value2.substring (0, value2.indexOf ("-")); month2 = value2.substring (value2.indexOf ("-")+1, value2.lastIndexOf ("-")); year2 = value2.substring (value2.lastIndexOf ("-")+1, value2.length); if (year1 > year2) return 1; else if (year1 < year2) return -1; else if (month1 > month2) return 1; else if (month1 < month2) return -1; else if (date1 > date2) return 1; else if (date1 < date2) return -1; else return 0; } /** * Checks if string is a number * @param {string} string to check * @param {object} objects with possible parameters * @returns {boolean} */ YAHOO.VAL.Validate.number = function(number, object) { if(number.length == 0) return -1; var decimals = (object.decimals) ? object.decimals : false; var minNr = (object.min) ? object.min : false; var maxNr = (object.max) ? object.max : false; var negative = (object.negative === "0") ? true : false; // replace all comma's with dot number = number.replace(/\./g, ","); var regExpr = "^[+-]?[0-9]*[,]?[0-9]*$"; regExpression = new RegExp(regExpr,"g"); YAHOO.log("number regExpression= " + regExpression,"info"); isNumber = regExpression.test(number); if(isNumber) { if(decimals) { decimals = Number(decimals); dot = number.lastIndexOf(","); if(dot !== -1) { substr = number.substring(dot+1); YAHOO.log("seachr decimals= " + substr,"info"); if(decimals == 0) return -6; if(substr.length > decimals) return -5; } } number = Number(number); if(minNr) { minNr = Number(minNr); YAHOO.log("number min " + number + " ?< " + minNr + " = " + ((number+0) < minNr),"info"); if(number < minNr) return -3; } if(maxNr) { maxNr = Number(maxNr); YAHOO.log("number max " + number + " ?> " + maxNr + " = " + ((number+0) > maxNr),"info"); if(number > maxNr) return -4; } return true; } else return false; } YAHOO.VAL.Validate.isset = function(v) { return (typeof( window[v] ) != "undefined") ? true : false; } /** * check 9 number digit with modulus 97 * @param {string} the number to be checked * @returns boolean */ YAHOO.VAL.Validate.doMod = function(number,control,extractor) { YAHOO.log("doMod :" + number + " - " + control,"info"); if(!YAHOO.VAL.Validate.isInteger(number)) return false; if(!YAHOO.VAL.Validate.isInteger(control)) return false; check = (extractor) ? extractor-(number%97) : number%97; ret = (control == check) ? true : false; YAHOO.log("mod check: " + control + " ?= " + check,"info"); return ret; }; YAHOO.VAL.Validate.isInteger = function(str) { regExpr = new RegExp("^\d+$","g"); ret = regExpr.test(str); YAHOO.log("is integer: " + str + " = " + ret,"info"); return ret; };