js正则验证日期(包括闰年)
function CheckObjDate(word)
{
var checkResult = CheckDate(word);
switch(checkResult)
{
case 'ERRORNODATE':
alert('不能为空!');
break;
case 'ERRORDATEFORMAT':
alert('时间格式不正确!');
break;
default:
alert('通过!');
return true;
}
}
function CheckDate(checkDate)
{
var reg =/^(?:(?:(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\/|-)(?:0?2\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\d)?\d{2})(\/|-)(?:(?:(?:0?[13578]|1[02])\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\2(?:0?[1-9]|1\d|2[0-8]))))$/;
var word = checkDate;
if(word.length == 0)
{
return 'ERRORNODATE';
}
var splitedWord = word.split("-");
if(splitedWord.length > 3 || splitedWord.length < 2)
{
return 'ERRORDATEFORMAT';
}
else
{
for(var i = 0; i < splitedWord.length; i++)
if(splitedWord[i].length == 0)
return 'ERRORDATEFORMAT';
}
if(splitedWord.length == 2)
{
if(splitedWord[0].length < 1 || splitedWord[0].length > 2)
return 'ERRORDATEFORMAT';
else if(splitedWord[0].length == 1)
{
splitedWord[0] = "0" + splitedWord[0];
}
if(splitedWord[1].length < 1 || splitedWord[1].length > 2)
return 'ERRORDATEFORMAT';
else if(splitedWord[1].length == 1)
{
splitedWord[1] = "0" + splitedWord[1];
}
var now = new Date();
word = now.getFullYear() + "/" + splitedWord[0] + "/" + splitedWord[1];
}
if(splitedWord.length == 3)
{
if(splitedWord[0].length < 1 || splitedWord[0].length > 4)
return 'ERRORDATEFORMAT';
else if(splitedWord[0].length == 1)
{
splitedWord[0] = "20