代码如下:
/*日期验证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 result=data.match(/((^((1[8-9]\d{2})|([2-9]\d{3}))(\/)(10|12|0?[13578])(\/)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(\/)(11|0?[469])(\/)(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(\/)(0?2)(\/)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)(\/)(0?2)(\/)(29)$)|(^([3579][26]00)(\/)(0?2)(\/)(29)$)|(^([1][89][0][48])(\/)(0?2)(\/)(29)$)|(^([2-9][0-9][0][48])(\/)(0?2)(\/)(29)$)|(^([1][89][2468][048])(\/)(0?2)(\/)(29)$)|(^([2-9][0-9][2468][048])(\/)(0?2)(\/)(29)$)|(^([1][89][13579][26])(\/)(0?2)(\/)(29)$)|(^([2-9][0-9][13579][26])(\/)(0?2)(\/)(29)$))/)将所有的(-)改成(\/)不就得了
我很久以前好像写过一个和这差不多的正则