// ================================================================= // validationDate. // // // // // // ================================================================= function validationDateClass(){ this.error = false; this.errorMsg = null; var daysInMonth = new Array(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; this.setError = function (msg,errorObject){ this.error = true; this.errorMsg = msg; errorObject.addError(this.errorMsg); } this.checkDate = function (day, month, year , errorObject){ if (isEmpty(year)) { this.setError("Please enter a Year.",errorObject); return false; } if (isEmpty(month)) { this.setError("Please enter a Month.",errorObject); return false; } if (isEmpty(day)) { this.setError("Please enter a Day of the month.",errorObject); return false; } if ((day == 31) && ((month == 2) || (month == 4) || (month == 6) || (month == 9) || (month == 11))) { this.setError("Please enter a valid Day and Month combination.",errorObject); return false; } if (day == 30 && month == 2) { this.setError("Please enter a valid Day and Month combination.",errorObject); return false; } // Leap year... if (day == 29 && month == 2) { if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { return true; } else { this.setError("Please enter a valid Day and Month combination.",errorObject); return false; } } if (!this.isInYear4DigitFormat(year+"")) { this.setError("Year must be a 4 digit numeric value.",errorObject); return false; } return true; } this.daysInFebruary = function(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); } this.isInDayFormat = function(value){ if (!isIntegerInRange(value, 1, 31)) { return false; } return true; } this.isInMonthFormat = function(value){ if (!isIntegerInRange(value, 1, 12)) { return false; } return true; } // this function expects year to be four digits this.isInYear4DigitFormat = function(value){ return isIntegerOfLength(value, 4); } this.isValidDayMonth = function(day, month){ var intMonth = parseInt(month, 10); var intDay = parseInt(day, 10); // catch invalid days, except for February if ((intDay > daysInMonth[intMonth])) { return false; } else { return true; } } this.isDateInFuture = function(day, month, year ){ var inFuture = false; var d = new Date(); var currentDay = d.getDate(); var currentMonth = d.getMonth() + 1; var currentYear = d.getFullYear(); var dayValue = parseInt(day, 10); var monthValue = parseInt(month, 10); var yearValue = parseInt(year, 10); if (yearValue > currentYear) { inFuture = true; } else { if ((yearValue == currentYear) && (monthValue > currentMonth)) { inFuture = true; } else { if ((yearValue == currentYear) && (monthValue == currentMonth) && (dayValue > currentDay)) { inFuture = true; } } } // if date is not in future, return false if (!inFuture) { return false; } return true; } this.isYearInFuture = function (value){ var d = new Date(); var currentYear = d.getFullYear(); var yearValue = parseInt(value, 10); if (yearValue > currentYear) { return true; } return false; } // ================================================================= // Private functions below // ================================================================= function isEmpty(s){ return ((s == null) || (s.length == 0)); } function isIntegerOfLength(value, requiredLength){ if (!isInteger(value)) { return false; } if (value.length != requiredLength) { return false; } return true; } function isInteger(s){ var i; var inputLen = s.length; if (inputLen == 0) { return false; } for (i = 0; i < inputLen; i++) { // Check that current character is number. var c = s.charAt(i); if (!isDigit(c)) { return false; } } // All characters are numbers. return true; } function isDigit(c) { return ((c >= "0") && (c <= "9")); } } var validationDate = new validationDateClass(); //tests //validationDate.checkDate("31","2","2000",error) //error.display();