JS函数 验证日期合法(测试通过)
<script type="text/javascript">
alert("2008-05-11是日期吗?答:"+isDateFormat("2008-05-11"));
function isDateFormat(txt){ //是否为合法的日期格式:YYYY-MM-DD
if(txt==null || txt == ""){return false;}
else{
var regex = /[0-9]{1,4}-[0-9]{1,2}-[0-9]{1,2}/; //可按具体格式修改
if( regex.test(txt) ){
var noArr = txt.split("-");
var year = eval(noArr[0]);
var month = eval(noArr[1]);
var day = eval(noArr[2]);
if ( year < 1 || month < 1 || month > 12 || day < 1 || day > 31) {return false;}
if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30){return false;}
if (month == 2){
if ((year % 4 != 0) && day > 29) { return false;}
if (year % 4 == 0){
if(year % 100 == 0 && year % 400 != 0 && day > 29){return false;}
else if (day > 28){ return false;}
}
}
return true;
}else{return false;}
}
}
</script>