欢迎来到福编程网,本站提供各种互联网专业知识!

js:日期正则表达式及检测

发布时间:2007-04-06 作者: 来源:转载
这是yyyy-mm-ddhh:mm:ss的/^(\d{4})\-(\d{2})\-(\d{2})(\d{2}):(\d{2}):(\d{2})$/;这是yyyy-mm-ddde的/^(\d{4})\-(\d{2})\-(\d{2})$/functionvalidateCNDate(strValue){varobjRegExp=/^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/if(!objRegExp.t
这是yyyy-mm-ddhh:mm:ss的
/^(d{4})-(d{2})-(d{2})(d{2}):(d{2}):(d{2})$/;
这是yyyy-mm-ddde的
/^(d{4})-(d{2})-(d{2})$/

functionvalidateCNDate(strValue){
varobjRegExp=/^d{4}(-|/|.)d{1,2}1d{1,2}$/


if(!objRegExp.test(strValue))
returnfalse;
else{
vararrayDate=strValue.split(RegExp.$1);
varintDay=parseInt(arrayDate[2],10);
varintYear=parseInt(arrayDate[0],10);
varintMonth=parseInt(arrayDate[1],10);

if(intMonth>12||intMonth<1){
returnfalse;
}

vararrayLookup={'1':31,'3':31,'4':30,'5':31,'6':30,'7':31,
'8':31,'9':30,'10':31,'11':30,'12':31}

if(arrayLookup[parseInt(arrayDate[1])]!=null){
if(intDay<=arrayLookup[parseInt(arrayDate[1])]&&intDay!=0)
returntrue;
}

if(intMonth-2==0){
varbooLeapYear=(intYear%4==0&&(intYear%100!=0||intYear%400==0));
if(((booLeapYear&&intDay<=29)||(!booLeapYear&&intDay<=28))&&intDay!=0)
returntrue;
}
}
returnfalse;
}

相关推荐