js时间验证
2013-12-01 11:25 卡尔丶 阅读(356) 评论(0) 编辑 收藏 举报<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function check(date)
{
//alert("sss");
//var date = document.getElementById("TextBox1").value;
var result = date.match(/((^((1[8-9]\d{2})|([2-9]\d{3}))(-)(10|12|0?[13578])(-)(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(11|0?[469])(-)(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))(-)(0?2)(-)(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)(-)(0?2)(-)(29)$)|(^([3579][26]00)(-)(0?2)(-)(29)$)|(^([1][89][0][48])(-)(0?2)(-)(29)$)|(^([2-9][0-9][0][48])(-)(0?2)(-)(29)$)|(^([1][89][2468][048])(-)(0?2)(-)(29)$)|(^([2-9][0-9][2468][048])(-)(0?2)(-)(29)$)|(^([1][89][13579][26])(-)(0?2)(-)(29)$)|(^([2-9][0-9][13579][26])(-)(0?2)(-)(29)$))/);
//alert("result"+result);
if(result==null)
{
alert("please input like \"yyyy-MM-dd\"");
return false;
}
else
{
return true;
}
}
</script>
<script type="text/javascript">
function myfun(type)
{
var startdate=document.getElementById("dateType_0").value;
var enddate=document.getElementById("dateType_1").value;
if(check(startdate)&&check(enddate))
{
var sDate=startdate;
var eDate=enddate;
var sArr = sDate.split("-");
var eArr = eDate.split("-");
var sRDate = new Date(sArr[0], sArr[1], sArr[2]);
var eRDate = new Date(eArr[0], eArr[1], eArr[2]);
var result = (eRDate-sRDate)/(24*60*60*1000);
alert(result);
if(result<0)
{
alert("enddate must later than or equal to startdate!");
return;
}
if(type==0)
{
if(result>30)
{
alert("You can't see the result more than 30 days by \"Day\"");
return;
}
}
if(type==1)
{
if(result>210)
{
alert("You can't see the result more than 30 days by \"Week\"");
return;
}
}
if(type==2)
{
if(result>900)
{
alert("You can't see the result more than 30 days by \"month\"");
return;
}
}
alert("Hello");
}
}
</script>
<body>
<input type="text" id="dateType_0">
<input type="text" id="dateType_1">
<a><span id="" onclick="myfun('0')">Day</span></a>
<a><span id="" onclick="myfun('1')">Week</span></a>
<a><span id="" onclick="myfun('2')">Month</span></a>
</body>
</html>