<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>正则表达式验证日期</title>
<script language="网页特效">
<!--
function checkform(form)
{
var rr=/^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1])) (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/
if(!rr.test(form.date.value))
{
alert('请输入正确的时间格式,如:2005-01-27 22:12:00');
form.date.focus();
return false;
}
alert('ok!');
return true;
}
代码二
function isdate(str){
var result=str.match(/^(d{4})(-|/)(d{1,2})2(d{1,2})$/);
if(result==null) return false;
var d=new date(result[1], result[3]-1, result[4]);
return (d.getfullyear()==result[1] && d.getmonth()+1==result[3] && d.getdate()==result[4]);
}
更多详细内容请查看:http://www.111cn.net/wy/99/33176.htm
代码三
function isvaliddate(stext) {
//yyyy-mm-dd
//var redate = /^((((1[6-9]|[2-9]d)d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]d|3[01]))|(((1[6-9]|[2-9]d)d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]d|30))|(((1[6-9]|[2-9]d)d{2})-0?2-(0?[1-9]|1d|2[0-8]))|(((1[6-9]|[2-9]d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29))$/
//dd-mm-yyyy
var redate = /^(((0?[1-9]|[12]d|3[01])-(0?[13578]|1[02])-((1[6-9]|[2-9]d)d{2}))|((0?[1-9]|[12]d|30)-(0?[13456789]|1[012])-((1[6-9]|[2-9]d)d{2}))|((0?[1-9]|1d|2[0-8])-0?2-((1[6-9]|[2-9]d)d{2}))|(29-0?2-((1[6-9]|[2-9]d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/
return redate.test(stext);
}
页面调用片段:
function validatedate(){
var begin = trim(document.getelementbyid("begindate").value);
if( begin != ""){
if(isvaliddate(begin) == false){
alert("==========");
return false;
}
}
}
//-->
</script>
</head>
<body>
</body>
</html>