求javascript代码 验证

来源:百度知道 编辑:UC知道 时间:2024/05/22 14:44:57
求一个验证例如:1920-1-1格式的javascript代码,也就是验证年-月-日,都是数字,而且有"-".

你最好用正则表达式,我写了个验证日期的(不是非常严格)你看一看能不能看懂,可以试一下。
<html>
<body>
<script language="javascript">
<!--
function test()
{
var pattern=/^[12]\d{3}-(0?\d|1[012])-(0?\d|[12]\d|3[01])$/;
var str=document.getElementById("str").value;
if(pattern.test(str)){alert("True!");}
else{alert("False!");}
}
-->
</script>
<input type="text" id="str">
<input type="button" value="Test" onclick="test()">
</body>
</html>