<%@ Page Language="C#" AutoEventWireup="true" CodeFile="datetimevalidate.aspx.cs"
Inherits="datetimevalidate" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript" language="javascript">
function fnOnclick()
{
var strDate = document.all.cnlDate.value;
if(!fnCheckDate(strDate))
{
alert("日期不合法");
}
else
{
alert("日期合法");
}
}
function fnCheckDate(strDate)
{
var strCheckDate = strDate+""; //進一步確認哪來判斷的肯定是一串字串
if(strCheckDate == "") //Null 字元串,不是合法的日期文字,返回false
{
return false;
}
debugger;
//判斷傳進來的資料是那種格式寫成日期
var intIndex = -1; //利用Regex,尋找字串中是否包含某個字元,沒找到為-1,否則為 (0 - String.length - 1)
var arrDate; //分別儲存年月日
var regExpInfo = //./; //Regex,匹配第一個出現 "."的位置
//在這裡,我之所以不使用replace函數把所有的"."和"/"換成"-",然後分別儲存年月日,是因為使用者有可能輸入 2001/3-2,就判斷不出它是不合法日期了
intIndex = strCheckDate.search(regExpInfo); //尋找是否含有 "."
if(intIndex == - 1) //不包含
{
regExpInfo = /-/;
intIndex = strCheckDate.search(regExpInfo);
if(intIndex == -1)
{
regExpInfo = ////; //尋找是否含有 "/"
intIndex = strCheckDate.search(regExpInfo);
if(intIndex == -1)
{
arrDate = new Array(); //只包含年或格式為20010307
if(strCheckDate.length==4)
{
arrDate[0]=strCheckDate;
window.alert(arrDate[0]);
}
else if(strCheckDate.length==6)
{
arrDate[0]=strCheckDate.substring(0,4);
arrDate[1]=strCheckDate.substring(4,6);
}
else if(strCheckDate.length==8)
{
arrDate[0]=strCheckDate.substring(0,4);
arrDate[1]=strCheckDate.substring(4,6);
arrDate[2]=strCheckDate.substring(6,8);
}
else
{
return false;
}
}
else
{
arrDate = strCheckDate.split("/"); //2001/3/7 型
}
}
else
{
arrDate = strCheckDate.split("-"); //2001-3-7 型
}
}
else
{
arrDate = strCheckDate.split("."); //2001.3.7 型
}
if(arrDate.length > 3) //如果分離出來的項超過3,除了年月日還有其它的,不合法日期,返回false
{
return false;
}
else if(arrDate.length > 0)
{
//判斷年是否合法
if(fnIsIntNum(arrDate[0])) //是正整數
{
if(parseInt(arrDate[0]) < 1 || parseInt(arrDate[0]) > 9999) //年範圍為1 - 9999
{
return false;
}
}
else
{
return false; //年不是正整數,錯誤
}
//判斷月是否合法
if(arrDate.length > 1)
{
if(fnIsIntNum(arrDate[1])) //是正整數
{
if(parseInt(arrDate[1]) < 1 || parseInt(arrDate[1]) > 12)
{
return false;
}
}
else
{
return false;
}
}
//判斷日是否合法
if(arrDate.length > 2)
{
if(fnIsIntNum(arrDate[2])) //是正整數
{
var intDayCount = fnComputerDay(parseInt(arrDate[0]),parseInt(arrDate[1]));
if(intDayCount < parseInt(arrDate[2]))
{
return false;
}
}
else
{
return false;
}
}
}
return true;
}
//**********************************************************************************************************
//判斷一個數是否為正整數
//參數:strNum ---- 需要判斷的字串
//傳回值:true ---- 整數 false ---- 非整數
function fnIsIntNum(strNum)
{
var strCheckNum = strNum + "";
if(strCheckNum.length < 1) //Null 字元串
return false;
else if(isNaN(strCheckNum)) //不是數值
return false;
else if(parseInt(strCheckNum) < 1) //不是正數
return false;
else if(parseFloat(strCheckNum) > parseInt(strCheckNum)) //不是整數
return false;
return true;
}
//**********************************************************************************************************
//功能:判斷intYear年intMonth月的天數
//傳回值:intYear年intMonth月的天數
function fnComputerDay(intYear,intMonth)
{
var dtmDate = new Date(intYear,intMonth,-1);
var intDay = dtmDate.getDate() + 1;
return intDay;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input name="cnlButton" id="cnlButton" type="button" value="日期檢查" onclick="fnOnclick()">
<asp:TextBox ID="cnlDate" runat="server"></asp:TextBox></div>
</form>
</body>
</html>