如何用Javascript判断文本框中输入的是不是日期格式
判断文本框中的内容是不是日期格式,或是不是正确的日期格式 问题点数:100、回复次数:6Top
1 楼fbj007(千里独行)回复于 2002-09-24 16:18:08 得分 0
表单递交合法性检测-日期:
<html>
<head>
<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>
</head>
<body onLoad="javascript:document.Form1.Date.focus()" bgcolor="#FFFFFF">
<form name="Form1" method="post" onSubmit="return testKey(event)" action="">
输入正确的日期(月/日/年):
<input type=text maxlength =10 name="Date" size=10 onBlur="formatDate(this)" value="">
</form>
</body>
</html>
说明:此脚本的用途是比较全面地检测输入日期的合法性,除了做非空检测外,还有效地检测了不同年月日期的合法性问题。比如在不是闰年的2月输入了29日等。黄色代码与脚本的检测无关,作用是页面读出页面后光标停留在日期文本框内。可以不要。
注意:(1)<Form>标签中表单的名字Form1和日期文本框的名字Data(加重字体)与脚本是有关的,也就是说你如果改动了它们的名字,凡是在脚本中引用From1和Data的部分都要修改。切切!!!
(2)Javascript是大小写敏感的,所以注意大小写的区别和一致性原则。
(3)此脚本应该与CGI/ASP等服务器端的递交处理程序配合使用,用于客户端的合法性检测。本例没有将submit按钮作上去,你所处理的表单中可能包括更多的内容。
这里仅仅提供了一个脚本思路,你不一顶非要全部照搬脚本,可以仅仅取脚本的一部分使用(主要是算法)。Top
2 楼tdl982324(最爱宝宝的笨熊)回复于 2002-09-24 16:24:02 得分 100
判断是否是日期
<script language=javascript>
function strDate(str){
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
var r = str.match(reg);
if(r==null)return false;
var d= new Date(r[1], r[3]-1,r[4]);
var newStr=d.getFullYear()+r[2]+(d.getMonth()+1)+r[2]+d.getDate()
return newStr==str
}
alert(strDate("2002-1-31"))
alert(strDate("2002-2-31"))
alert(strDate("2002-1-41"))
</script>Top
3 楼havenbird(冬 瓜)(蓝色婴孩)回复于 2002-09-24 16:50:08 得分 0
if (isNaN(document.baoing.year.value)||document.baoing.year.value > newDateObj.getFullYear()||document.baoming.year.value < 1800)
{
alert("请输入正确的出生年份");
document.baoming.year.focus();
return false;
}
if (isNaN(document.baoming.month.value)||document.baoming.month.value >12||document.baoming.month.value < 1)
{
alert("请输入正确的出生月份");
document.baoming.month.focus();
return false;
}
if (isNaN(document.baoming.day.value) || document.baoming.day.value > 31 || document.baoming.day.value < 1)
{
alert("请输入正确的出生日期");
document.baoming.day.focus();
return false;
}
if (!isDate(document.baoming.year.value, document.baoming.month.value, document.baoming.day.value))
{
alert("日期组合错误,请检查您的日期组合。\r\n\r\n[提示,某些月份没有31日,二月没有30日,或没有29日]");
document.baoming.day.focus();
return false;
}
表单提交的时候调用这个函数Top
4 楼chenxy2002((HomeMan)回复于 2002-09-24 16:59:45 得分 0
gz
================================================================
CSDN 论坛助手 Ver 1.0 B0402提供下载。 改进了很多,功能完备!
★ 浏览帖子速度极快![建议系统使用ie5.5以上]。 ★ 多种帖子实现界面。
★ 保存帖子到本地[html格式]★ 监视您关注帖子的回复更新。
★ 可以直接发贴、回复帖子★ 采用XML接口,可以一次性显示4页帖子,同时支持自定义每次显示帖子数量。可以浏览历史记录!
★ 支持在线检测程序升级情况,可及时获得程序更新的信息。
★★ 签名 ●
可以在您的每个帖子的后面自动加上一个自己设计的签名哟。
Http://www.ChinaOK.net/csdn/csdn.zip
Http://www.ChinaOK.net/csdn/csdn.rar
Http://www.ChinaOK.net/csdn/csdn.exe [自解压]
Top
5 楼chenxy2002((HomeMan)回复于 2002-09-24 17:00:14 得分 0
gzTop
6 楼abigfrog(千年精灵)(★JAVA★)回复于 2002-09-24 17:06:38 得分 0
//函数名:chkdate
//功能介绍:检查是否为日期
//参数说明:要检查的字符串
//返回值:0:不是日期 1:是日期
function chkdate(datestr)
{
var lthdatestr
if (datestr != "")
lthdatestr= datestr.length ;
else
lthdatestr=0;
var tmpy="";
var tmpm="";
var tmpd="";
//var datestr;
var status;
status=0;
if ( lthdatestr== 0)
return 0
for (i=0;i<lthdatestr;i++)
{ if (datestr.charAt(i)== '-')
{
status++;
}
if (status>2)
{
//alert("Invalid format of date!");
return 0;
}
if ((status==0) && (datestr.charAt(i)!='-'))
{
tmpy=tmpy+datestr.charAt(i)
}
if ((status==1) && (datestr.charAt(i)!='-'))
{
tmpm=tmpm+datestr.charAt(i)
}
if ((status==2) && (datestr.charAt(i)!='-'))
{
tmpd=tmpd+datestr.charAt(i)
}
}
year=new String (tmpy);
month=new String (tmpm);
day=new String (tmpd)
//tempdate= new String (year+month+day);
//alert(tempdate);
if ((tmpy.length!=4) || (tmpm.length>2) || (tmpd.length>2))
{
//alert("Invalid format of date!");
return 0;
}
if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) )
{
//alert ("Invalid month or day!");
return 0;
}
if (!((year % 4)==0) && (month==2) && (day==29))
{
//alert ("This is not a leap year!");
return 0;
}
if ((month<=7) && ((month % 2)==0) && (day>=31))
{
//alert ("This month is a small month!");
return 0;
}
if ((month>=8) && ((month % 2)==1) && (day>=31))
{
//alert ("This month is a small month!");
return 0;
}
if ((month==2) && (day==30))
{
//alert("The Febryary never has this day!");
return 0;
}
return 1;
}
Top




