js 日期验证 .NET
function CheckDate("obj=2012年01月11日"||"obj=2012-01-11"||"obj=20120111")
{
var date = obj;
var year,month,day,datePat,matchArray;
if(/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})$/.test(date)){
datePat = /^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
year=date.substring(0,4);
month=date.substring(5,7);
day=date.substring(8,10);
}
else if(/^(\d{4})年(\d{1,2})月(\d{1,2})日$/.test(date)){
datePat = /^(\d{4})年(\d{1,2})月(\d{1,2})日$/;
year=date.substring(0,4);
month=date.substring(5,7);
day=date.substring(8,10);
}
else if(/^(\d{4})(\d{2})(\d{2})$/.test(date)){
datePat = /^(\d{4})(\d{2})(\d{2})$/;
year=date.substring(0,4);
month=date.substring(4,6);
day=date.substring(6,8);
}
else
{
alert("日期格式不对");
return false;
}
matchArray = date.match(datePat);
if(matchArray == null)
{
alert("日期长度不对,或日期中有非数字符号");
return false;
}
if (month < 1 || month > 12)
{
alert("月份应该为1到12的整数");
return false;
}
if (day < 1 || day > 31)
{
alert("每个月的天数应该为1到31的整数");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31)
{
alert("该月不存在31号");
return false;
}
if (month==2)
{
var isleap=(year % 4==0 && (year % 100 !=0 || year % 400==0));
if (day>29)
{
alert("2月最多有29天");
return false;
}
if ((day==29) && (!isleap))
{
alert("闰年2月才有29天");
return false;
}
}
return true;
}