<script type="text/javascript" language="javascript">
function isLeapYear() {
return (year % 4 != 0 ? false :
( year % 100 != 0? true:
( year % 1000 != 0? false : true)));
}
function isValidDate(date_string) {
var days = [0,31,28,31,30,31,30,31,31,30,31,30,31];
var year, month, day, date_parts = null;
var rtrn = false;
var format = 'mm/dd/yyyy';
var decisionTree = {
'm/d/y':{
're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{2}|\d{4})$/,
'month': 1,'day': 2, year: 3
},
'mm/dd/yy':{
're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{2})$/,
'month': 1,'day': 2, year: 3
},
'mm/dd/yyyy':{
're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{4})$/,
'month': 1,'day': 2, year: 3
},
'y/m/d':{
're':/^(\d{2}|\d{4})[./-](\d{1,2})[./-](\d{1,2})$/,
'month': 2,'day': 3, year: 1
},
'yy/mm/dd':{
're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{1,2})$/,
'month': 2,'day': 3, year: 1
},
'yyyy/mm/dd':{
're':/^(\d{4})[./-](\d{1,2})[./-](\d{1,2})$/,
'month': 2,'day': 3, year: 1
}
};
var test = decisionTree[format];
if (test) {
date_parts = date_string.match(test.re);
if (date_parts) {
year = date_parts[test.year] * 1;
month = date_parts[test.month] * 1;
day = date_parts[test.day] * 1;
test = (month == 2 &&
isLeapYear() &&
29 ||
days[month] || 0);
rtrn = 1 <= day && day <= test;
}
}
return rtrn;
}
</script>
No comments:
Post a Comment