关闭

JavaScript Date Validation

196人阅读 评论(0) 收藏 举报
function isDate(value) {
try {
value = value.replace("-", "/").replace(".", "/");
var SplitValue = value.split("/");
var OK = true;
if (!(SplitValue[0].length == 1 || SplitValue[0].length == 2)) {
OK = false;
}
if (OK && !(SplitValue[1].length == 1 || SplitValue[1].length == 2)) {
OK = false;
}
if (OK && SplitValue[2].length != 4) {
OK = false;
}
if (OK) {
var Day = parseInt(SplitValue[0]);
var Month = parseInt(SplitValue[1]);
var Year = parseInt(SplitValue[2]);

if (OK = ((Year > 1900) && (Year < new Date().getFullYear()))) {
if (OK = (Month <= 12 && Month > 0)) {
var LeapYear = (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0));

if (Month == 2) {
OK = LeapYear ? Day <= 29 : Day <= 28;
}
else {
if ((Month == 4) || (Month == 6) || (Month == 9) || (Month == 11)) {
OK = (Day > 0 && Day <= 30);
}
else {
OK = (Day > 0 && Day <= 31);
}
}
}
}
}
return OK;
}
catch (e) {
return false;
}
}

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8738次
    • 积分:199
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档