|
This 17 message thread spans 2 pages: < < 1 [2]
|
|
!isNaN((new Date(1/1/05)).getYear()) won't work because the Date() constructor is too generous. For example alert(new Date("02/31/2007")) = March 3rd and even 3/3000/3 is considered an acceptable date (Wed May 17 1911).
|
|
|
function isISODate (value) {
var dates = value.split("-");
if (dates.length >= 3) {
var ds = dates[1] + "-" + dates[2] + "-" + dates[0];
var test = new Date (ds);
if (isNaN(test)) {
alert('Enter a valid date in the correct format: YYYY-MM-DD');
return false;
} else {
if (test.getFullYear() != (dates[0] * 1) || (test.getMonth()+1) != (dates[1] * 1) || test.getDate() != (dates[2] * 1)) {
alert('This is not a valid date: ' + value);
return false;
} else {
return true;
}
}
} else {
alert('Enter a valid date in the format: YYYY-MM-DD');
return false;
}
}
An ISO date is in the format YYYY-MM-DD, but javascript doesn't read that format. The string "ds" rearranges the date to MM-DD-YYYY. The getMonth() function returns the month as an integer from 0-11, hence "+1". Multipling the variable "dates" by 1 converts it into an integer.
This rutin checks that the format is right, that the position of the year, month and day is correct, and that the numbers given correspond to the intended date, since the javascript "date" function will add or remove days as appropriate in order to adjust to the closest possible date of that intended.
Fred
|
|
This 17 message thread spans 2 pages: < < 1 [2] |
|
|
|
|
|