js:日期正则表达式及检测
程序员文章站
2022-03-27 12:12:32
这是yyyy-mm-dd hh:mm:ss 的 /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2...
这是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;
}
上一篇: 正则表达式检查来访IP是否合法的实际应用