代码如下:
/*日期验证dd/mm/yyyy*/
functionisDate(str){
vardate=str
varresult=date.match(/^([0]?[1-9]|[1|2][0-9]|[3][0|1])[/]([0]?[1-9]|[1][0-2])[/]([0-9]{4}|[0-9]{2})$/)
if(result==null)
returnfalse
vard=newDate(result[3],result[2]-1,result[1])
return(d.getFullYear()==result[3]&&(d.getMonth()+1)==result[2]&&d.getDate()==result[1])
}
扩展资料
java用正则表达式判断输入的日期格式为yyyy/MM/dd
publicstaticvoidmain(String[]args)throwsParseException{
Stringstr1="2012-05-15"
Stringstr2="2012/05/15"
if(zzbd(str1)){
System.out.println("str1havematch!")
}else{System.out.println("str1don'tmatch!")}
if(zzbd(str2)){
System.out.println("str2havematch!")
}else{System.out.println("str2don'tmatch!")}}
publicstaticbooleanzzbd(Strings)
{
Patternpattern=Pattern.compile(\\d{4}/\\d{2}/\\d{2})
Matchermatcher=pattern.matcher(s)
if(matcher.find())
{
returntrue
}else{
returnfalse
}
}
}
var reg1 = /^(([1-9]\d{3})\-(((0[13578]|1[02])\-(0[1-9]|[12]\d|3[01]))|((0[469]|11)\-(0[1-9]|[12]\d|3[0]))|(02\-(0[1-9]|1\d|2[0-8])))|([1-9]\d{3})\/(((0[13578]|1[02])\/(0[1-9]|[12]\d|3[01]))|((0[469]|11)\/(0[1-9]|[12]\d|3[0]))|(02\/(0[1-9]|1\d|2[0-8]))))$/
var reg2 = /^(0|([1-9]\d*)|)$/
var reg3 = /^((\d\:\d{2}\s+(am|AM|pm|PM))|)$/
console.log(reg1.test('2014/11/20'))
console.info(reg1.test('2014-11-20'))
console.debug(reg2.test('1'))
reg2.test('')
reg3.test('0:00 am')