javascript对日期的验证代码

作者在 2010-11-09 10:52:56 发布以下内容
function validateDate(date) {
    var pos1 = date.indexOf("-");
    var pos2 = date.indexOf("-", pos1 + 1);
    if ( pos1 == -1 || pos2 == -1 ) {
        return false;
    }
    var year = date.substr(0, pos1);
    var month = date.substr(0, pos1);
    var day = date.substr(0, pos1);
    if ( month < 1 || month > 12 ) {
        return false;
    }
    if ( day < 1 || day > 31 ) {
        return false;
    }
    if ( (month == 4 || month == 6 || month == 11) && (day == 31) ) {
        return false;
    }
    if ( month == 2 ) {
        var leap = (year % 4 == 0 && (year %100 != 0 || year % 400 == 0));
        if (day > 29 || (day == 29 && !leap)) {
            return false;
        }
    }
    return true;
}
默认分类 | 阅读 733 次
文章评论,共2条
小K鸟
2010-11-12 21:17
1
这个功能有点儿强大了啊,我只弄过很简单的东东,,,
懂也不懂
2010-12-01 17:51
2
用其他语言写过,就是还没用JS写,现在正在学JS
游客请输入验证码