代码如下:
/*日期验证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
}
}
}
/^\d{4,}\/(?:0?\d|1[12])\/(?:[012]?\d|3[01]) (?:[01]?\d|2[0-4]):(?:[0-5]?\d|60)$/.test('2015/11/3 12:24')