</head>
<body>
<input name="cnldate" id="cnldate" value="">(如:2001-3-7 2001/3/7 2001.3.7)
<br>
<input name="cnlradio" id="radio" value="0" type="radio" checked>年、月、日必须齐全<br>
<input name="cnlradio" id="radio" value="1" type="radio">可以没有日<br>
<input name="cnlradio" id="radio" value="2" type="radio">可以没有月和日<br>
<input name="cnlbutton" name="cnlbutton" type="button" value="日期检查" onclick="fnonclick()">
</body>
</html>
<script language="javascript" src="scroll.js">
//**********************************************************************************************************
function fnonclick()
{
var strdate = fnremovebrank(document.all.cnldate.value);
var intcheckradio = 0;
if(document.all.cnlradio[1].checked)
{
intcheckradio = 1;
}
else if(document.all.cnlradio[2].checked)
{
intcheckradio = 2;
}
if(!fncheckdate(strdate,intcheckradio))
{
alert("日期不合法");
}
else
{
alert("日期合法");
}
}
//**********************************************************************************************************
//功能:日期检查函数,支持3种年、月、日之间的分隔符 "-"、"."和"/"可以选择年、月、日是否应该完整。
// 正确的日期格式为:2001-2-13 2001 2001-2 2001.2.13 2001.2 2001/2/3,日期范围为 1-1-1 到 9999-12-31
// 同时,对当前年当前月的天数也做了判断,如:2001-2-29 2001-4-31 都是非法的日期
//参数:strdate ---- 需要判断的日期字符串
// intflag: 1 ---- 可以没有日 2 ---- 可以没有日和月 0 ---- 年月日必须齐全
//返回值:true ---- 日期合法 false ---- 日期不合法
function fncheckdate(strdate,intflag)
{
var strcheckdate = strdate + ""; //进一步确认哪来判断的肯定是一串字符串
if(strcheckdate == "") //空字符串,不是合法的日期字符串,返回false
{
return false;
}
//判断传进来的数据是那种格式写成日期
var intindex = -1; //利用正则表达式,查找字符串中是否包含某个字符,没找到为-1,否则为 (0 - string.length - 1)
var arrdate; //分别存储年月日
var regexpinfo = /\./; //正则表达式,匹配第一个出现 "."的位置
//在这里,我之所以不使用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(strcheckdate); //只包含年
}
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;
}
}
else //没有月
{