HTML的文本中只允许有日期输入
?
<script>
//驗證給定的日期是否合法?????? ,參數格式要求:yyyy-mm-dd?? 可以根據情況更改正則表達式
function isDate(oStartDate)
{
??? //對日期格式進行驗證?? 要求為2000-2099年???? 格式為?? yyyy-mm-dd?? 并且可以正常轉換成正確的日期
??? var pat_hd=/^20\d{2}-((0[1-9]{1})|(1[0-2]{1}))-((0[1-9]{1})|([1-2]{1}[0-9]{1})|(3[0-1]{1}))$/;
??? try
??? {??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
??????? if(!pat_hd.test(oStartDate)){throw "日期非法! "; return;}
??????? var arr_hd=oStartDate.split( "-");
??????? var dateTmp;
??????? dateTmp=new Date(arr_hd[0],parseFloat(arr_hd[1])-1,parseFloat(arr_hd[2]));
??????? if(dateTmp.getFullYear()!=parseFloat(arr_hd[0]) || dateTmp.getMonth()!=parseFloat(arr_hd[1])-1 || dateTmp.getDate()!=parseFl
oat(arr_hd[2]))
??????? {
??????????? throw "日期非法! ";
??????????? return ;
??????? }
??? }
??? catch(ex)
??? {
??????? if(ex.description)
??????? {
??????????? return false;
??????? }
??????? else
??????? {
??????????? return false;
??????? }
??? }
??? return true;
}
</script>
<form? method="get">
???? 查詢日期(格式:20120808): <input type="text" name="begin_time"? onpropertychange="isDate(this)" οninput="isDate(this)"/>
???? 至: <input type="text" name="end_time"? onpropertychange="isDate(this)" οninput="isDate(this)"/>
??? <input type="submit"/>
</form>
<?php
header("Content-Type: text/html; charset=UTF-8");
if($_GET["begin_time"] >= $_GET["end_time"])
//if($_GET["begin_time"] == NULL || $_GET["end_time"] == NULL || $_GET["begin_time"] >= $_GET["end_time"])
{
??? echo "結束時間早于開始時間!\n";
??? return;
}
總結
以上是生活随笔為你收集整理的HTML的文本中只允许有日期输入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 报错expected specifier
- 下一篇: 学习正则表达式点滴