用JavaScript验证日期格式

程序代码

<form name="datecheck" method="POST"  onsubmit="return dateCheck(this.date.value);">
<table border="0" cellpadding="8" cellspacing="0" width="50%">
<tr>
    <td align="right" nowrap>Birth Date (DD/MM/YYYY):</td>
    <td><input type=text name="date" size="25"></td>
</tr>
<tr>
    <td class="center" colspan="2">
        <input type=submit value="发送">
        <input type=reset value="重写">
    </td>
</tr>
</table>
</form>
<script type="text/javascript" language="javascript">
function dateCheck(str){
    var re = new RegExp("^([0-9]{1,2})[./]{1}([0-9]{1,2})[./]{1}([0-9]{4})$");
    var ar;
    var res = true;
    
    if ((ar = re.exec(str)) != null){
        var i;
        i = parseFloat(ar[1]);
        // 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('请输入 DD/MM/YYYY 日期格式');
    }
    else{
     alert("success");
    }
    return res;
}
</script>

验证日期格式是否为dd/mm/yyyy

你可能感兴趣的:(JavaScript)