<script language=javascript>
function strDateTime(str)
{
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)return false;
var d= new Date(r[1], r[3]-1, r[4]);
return (d.getFullYear()==r[1] && (d.getMonth()+1)==r[3] && d.getDate()==r[4]);
}
alert(strDateTime("2002-01-31"));
alert(strDateTime("2002-01-41"));
</script>
function strDateTime(str)
{
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)return false;
var d= new Date(r[1], r[3]-1, r[4]);
return (d.getFullYear()==r[1] && (d.getMonth()+1)==r[3] && d.getDate()==r[4]);
}
alert(strDateTime("2002-01-31"));
alert(strDateTime("2002-01-41"));
</script>
{
var strValue = new String();
var year = new String();
var month = new String();
var day = new String();
strValue = value;
if (strValue.length!=10)
{
return false;
}
else
if (strValue.charAt(4)!='-'||strValue.charAt(7)!='-')
{
return false;
}
else
{
year = strValue.substr(0,4);
month = strValue.substr(5,2);
month = month-1;
day = strValue.substr(8,2);
var testDate = new Date(year,month,day)
//alert(testDate);
return (year == testDate.getFullYear()) && (month ==testDate.getMonth())&&(day == testDate.getDate());
}
return true;
}
alert(0==y%4 && (y%100!=0 || y%400==0));
其中,check是在javascript中定义的function
为什么接受不到form中的变量
{
try{
//strValue 被检测的日期字符
strValue=new String(strValue);
var Reg;
var iDotPos,iYear,strRight,iMonth,iDay;
//日期的正则表达式,d(n)表示必须有n个数字,"|"表示或的关系,“\/"是转意符表示字符"/",
//以下字符的意思是符合2002/7/30 ,02/07/3.....等形式的字符表达式
Reg=/^(\d{2}|\d{4})\/(\d{1}|\d{2})\/(\d{1}|\d{2})$/;
if(Reg.test(strValue)) //检测是否匹配
{
iDotPos=strValue.search("\/");
iYear=strValue.substring(0,iDotPos); //读出年份
strRight=strValue.substring(iDotPos+1,strValue.length);
iDotPos=strRight.search("\/");
iMonth=strRight.substring(0,iDotPos); //读出月份
iDay=strRight.substring(iDotPos+1,strRight.length); //读出日
if(iMonth>12||iMonth==0)
return false;
if(iMonth==2)
{
if (iYear<100) iYear+=2000;
if ((iYear % 4 == 0 && iYear % 100 != 0) || iYear % 400 == 0) //检测是否闰年
if(iDay>29||iDay==0) return false;
else
if(iDay>28||iDay==0) return false;
return true;
}
else if(iMonth==1 || iMonth==3 || iMonth == 5 ||iMonth==7 || iMonth==8 || iMonth==10 || iMonth==12)
if(iDay>31||iDay==0)
return false;
else
if(iDay>30||iDay==0)
return false;
return true;
}
else
return false;
}catch(e)
{
alert(e.description);
return false;
}
}