`

正则表达式 验证日期

 
阅读更多

使用正则表达式验证日期格式是否正确。

正确的格式为:2011-07-13

checkdate.jsp 如下

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>检查日期</title>
<script src="<%=basePath%>/js/jquery-1.4.4.js"></script>
<script type="text/javascript">

 function checkDate(){
 var date=$("#date ").val();
 var checkStr= /^((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)$/;
 
 if(!checkStr.test(date)){
   alert("您输入的日期有误(2010-02-12),请重新输入!");
  }else{
   alert("日期输入正确! ");
   }
 }

</script>


</head>
<body>

<div>
<label>请输入日期:</label>
<input style="text" id="date"/>
</div>
<div>
<input type="button" value="检测" onclick="checkDate()"/>
</div>
</body>
</html>

 :

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics