JavaScript正則表達式
Code
<html>
<head>
<title>Insert title here</title>
<script type="text/javascript" language="javascript">
function dateCheck(){
var date = document.all('date').value;
var re = new RegExp("^([0-9]{4})[./-]{1}([0-9]{1,2})[./-]{1}([0-9]{1,2})$");
var ar;
var res = true;
if ((ar = re.exec(date)) != null)
{
var i;
i = parseFloat(ar[3]);
// verify dd
if (i <= 0 || i > 31){
res = false;
}
i = parseFloat(ar[2]);
// verify mm
if (i <= 0 || i > 12){
res = false;
}
}
else
{
res = false;
}
if (!res){
alert('!res请输入 yyyy-mm-dd 日期格式');
}
else
{
alert("success");
}
return res;
}
</script>
</head>
<body>
<form name="datecheck" method="POST" >
<table border="0" cellpadding="8" cellspacing="0" width="50%">
<tr>
<td align="right" nowrap>Birth Date (DD YY):</td>
<td><input type=text name="date" size="25"></td>
</tr>
<tr>
<td class="center" colspan="2">
<input type=button value="发送" onclick='dateCheck()'>
<input type=reset value="重写">
</td>
</tr>
</table>
</form>
</body>
</html>
<html>
<head>
<title>Insert title here</title>
<script type="text/javascript" language="javascript">
function dateCheck(){
var date = document.all('date').value;
var re = new RegExp("^([0-9]{4})[./-]{1}([0-9]{1,2})[./-]{1}([0-9]{1,2})$");
var ar;
var res = true;
if ((ar = re.exec(date)) != null)
{
var i;
i = parseFloat(ar[3]);
// verify dd
if (i <= 0 || i > 31){
res = false;
}
i = parseFloat(ar[2]);
// verify mm
if (i <= 0 || i > 12){
res = false;
}
}
else
{
res = false;
}
if (!res){
alert('!res请输入 yyyy-mm-dd 日期格式');
}
else
{
alert("success");
}
return res;
}
</script>
</head>
<body>
<form name="datecheck" method="POST" >
<table border="0" cellpadding="8" cellspacing="0" width="50%">
<tr>
<td align="right" nowrap>Birth Date (DD YY):</td>
<td><input type=text name="date" size="25"></td>
</tr>
<tr>
<td class="center" colspan="2">
<input type=button value="发送" onclick='dateCheck()'>
<input type=reset value="重写">
</td>
</tr>
</table>
</form>
</body>
</html>