📄 datevalid.html
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script Language="JavaScript">
<!--
function testKey(e){
chars= "0123456789/";
e = window.event;
if(chars.indexOf(String.fromCharCode(e.keyCode))==-1) window.event.keyCode=0;
};
function valDate(M, D, Y){
Months= new Array(31,28,31,30,31,30,31,31,30,31,30,31);
Leap = false;
if((Y % 4 == 0) && ((Y % 100 != 0) || (Y %400 == 0)))
Leap = true;
if((D < 1) || (D > 31) || (M < 1) || (M > 12) || (Y < 0))
return(false);
if((D > Months[M-1]) && !((M == 2) && (D > 28)))
return(false);
if(!(Leap) && (M == 2) && (D > 28))
return(false);
if((Leap) && (M == 2) && (D > 29))
return(false);
};
function formatDate(dateForm){
cDate = dateForm.value;
dSize = cDate.length;
sCount= 0;
if (document.Form1.Date.value == ""){
alert("请输入日期!");
return false ;
}
if(cDate=='') return;
for(var i=0; i < dSize; i++)
(cDate.substr(i,1) == "/") ? sCount++ : sCount;
if (sCount != 2){
alert("输入的日期格式必须是\n ''月/日/年''");
dateForm.select();
return(false);
};
//检测输入的年份是2位数还是4位数﹔
ySize = cDate.substring(cDate.lastIndexOf("/")+1,dSize).length
if(ySize<2 || ySize>4 || ySize == 3){
alert('您输入的日期错误 !');
dateForm.select();
return false;
};
//将输入的日期字符串分隔成3部分 (Month, Day & Year)
idxBarI = cDate.indexOf("/");
idxBarII= cDate.lastIndexOf("/");
strM = cDate.substring(0,idxBarI);
strD = cDate.substring(idxBarI+1,idxBarII);
strY = cDate.substring(idxBarII+1,dSize);
strM = (strM.length < 2 ? '0'+strM : strM);
strD = (strD.length < 2 ? '0'+strD : strD);
if(strY.length == 2)
strY = (strY > 50 ? '19'+strY : '20'+strY);
dateForm.value = strM+'/'+strD+'/'+strY;
ok = valDate(strM, strD, strY);
if(ok==false){
alert("您输入的日期错误 !");
dateForm.select();
return false;
};
};
-->
</script>
<title>日期合法性检测</title>
<link rel="stylesheet" href="../style.css">
<meta name="keywords" content="valid, validation, check, date, form, submit, 合法性, 合法, 检测, 日期, 递交, 窗体, 窗体递交">
</head>
<body onLoad="javascript:document.Form1.Date.focus()" bgcolor="#000066" text="#FFFFFF" link="#FFFFFF" vlink="#FFFFFF" alink="#FFFFFF">
<form name="Form1" method="post" onSubmit="return testKey(event)" action="">
<div align="center">输入正确的日期格式(月/日/年):
<input type=text maxlength =10 name="Date" size=10 onBlur="formatDate(this)" value="">
</div>
</form>
<div align="center">
<p>(测试:输入错误的日期格式后,用鼠标在外面随便点点看。)</p>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -