js:日期正则表达式及检测
(编辑:jimmy 日期: 2024/11/18 浏览:3 次 )
这是yyyy-mm-dd hh:mm:ss 的
/^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/ ;
这是 yyyy-mm-ddde 的
/^(\d{4})\-(\d{2})\-(\d{2})$/
function validateCNDate( strValue ) {
var objRegExp = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
if(!objRegExp.test(strValue))
return false;
else{
var arrayDate = strValue.split(RegExp.$1);
var intDay = parseInt(arrayDate[2],10);
var intYear = parseInt(arrayDate[0],10);
var intMonth = parseInt(arrayDate[1],10);
if(intMonth > 12 || intMonth < 1) {
return false;
}
var arrayLookup = { '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)
return true;
}
if (intMonth-2 ==0) {
var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
return true;
}
}
return false;
}
/^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/ ;
这是 yyyy-mm-ddde 的
/^(\d{4})\-(\d{2})\-(\d{2})$/
function validateCNDate( strValue ) {
var objRegExp = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
if(!objRegExp.test(strValue))
return false;
else{
var arrayDate = strValue.split(RegExp.$1);
var intDay = parseInt(arrayDate[2],10);
var intYear = parseInt(arrayDate[0],10);
var intMonth = parseInt(arrayDate[1],10);
if(intMonth > 12 || intMonth < 1) {
return false;
}
var arrayLookup = { '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)
return true;
}
if (intMonth-2 ==0) {
var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
return true;
}
}
return false;
}
下一篇:正则表达式,相关链接