不用你那么麻烦吧,看看下面的,是不是合用?<script language=javascript>
function check() {
re = /^(19|20)\d{2}\-\d{2}\-\d{2}$/;
if(!re.test(form1.newDate.value)){
alert("Error format.");
form1.newDate.focus();
return false;
}
return true;
}
</script>
<form name="form1" method="post" action="" onsubmit="return check();">
<input type="text" name="newDate">2004-04-07<p>
<input name="Submit" type="submit" class="box" value="提交">
<input name="Submit2" type="reset" class="box" value="重置"></form>
function check() {
re = /^(19|20)\d{2}\-\d{2}\-\d{2}$/;
if(!re.test(form1.newDate.value)){
alert("Error format.");
form1.newDate.focus();
return false;
}
return true;
}
</script>
<form name="form1" method="post" action="" onsubmit="return check();">
<input type="text" name="newDate">2004-04-07<p>
<input name="Submit" type="submit" class="box" value="提交">
<input name="Submit2" type="reset" class="box" value="重置"></form>
//功能介绍:检查是否为日期//参数说明:datestr要检查的对象
//返回值: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;
for (i=0;i<lthdatestr;i++)
{ if (datestr.charAt(i)== '-')
{
status++;
}
if (status>2)
{
//alert("Invalid format of date!");
datestr.focus();
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))
{
return 0;
}
if (!((1<=month) && (12>=month) && (31>=day) && (1<=day) && (2050>=year) && (1850<=year)))
{
return 0;
}
if (!((year % 4)==0) && (month==2) && (day==29))
{
return 0;
}
if ((month<=7) && ((month % 2)==0) && (day>=31))
{
return 0;
}
if ((month>=8) && ((month % 2)==1) && (day>=31))
{
return 0;
}
if ((month==2) && (day==30))
{
return 0;
}
return 1;
}