try this:
<form name="f1">
<input type="text" name="user_date" value="">
<input type="button" onclick="check(text.value,)">
</form><script>
function check()
{
var reg=/\d{4}-\d{1,2}-\d{1,2}/
if(reg.test(f1.text.value)){return true}
else{return false}
var str=f1.text.value
var c_str=str.splite("-")
if(c_str[1]>12){return false} //检查月份的有效性
else if(c_str[2]>31){return fale} //检查日期的有效性
}
</script>
<form name="f1">
<input type="text" name="user_date" value="">
<input type="button" onclick="check(text.value,)">
</form><script>
function check()
{
var reg=/\d{4}-\d{1,2}-\d{1,2}/
if(reg.test(f1.text.value)){return true}
else{return false}
var str=f1.text.value
var c_str=str.splite("-")
if(c_str[1]>12){return false} //检查月份的有效性
else if(c_str[2]>31){return fale} //检查日期的有效性
}
</script>
{
if((year
%4==0&&year%100!=0)||(year%400==0))
{
return true;
} return false;
} 2#判定主函数 function dateValidation(object,format)
{
var regexp,value,index;
var year,month,day;
var iyear,imonth,iday;
var fmt,regfmt,ordfmt;
var dateArray; if(isObject(object))
{
value=object.value;
}
else if(isString(object)&&!isEmpty(object))
{
value=object;
}
else
{
return false;
}
if(isEmpty(format))
{
return false;
} fmt=new Array("yyyy/mm/dd","mm/dd/yyyy","dd/mm/yyyy"); regfmt=new Array("/^([0-9]{4})\\/([0-9]{2})\\/([0-9]{2})$/","/^([0-9]{2})\\/([0-9]{2})\\/([0-9]{4})$/","/^([0-9]{2})\\/([0-9]{2})\\/([0-9]{4})$/"); ordfmt=new Array("123","312","321"); format=format.toLowerCase();
for(index=0;index<fmt.length;index++)
{
if(format==fmt[index])
{
eval(''regexp=''+regfmt[index]+'';''); iyear=parseInt(ordfmt[index].charAt(0));
imonth=parseInt(ordfmt[index].charAt(1));
iday=parseInt(ordfmt[index].charAt(2)); break;
}
} if(index==fmt.length)
{
alert("Date Format Not Supported!");
return false;
} if(regexp.test(value)){
//alert("Date is matched with Format!");
dateArray=value.match(regexp); year=dateArray[iyear];
month=dateArray[imonth];
day=dateArray[iday]; //alert("The Date you have filled is:\nYear:"+year+"\nMonth:"+month+"\nDay:"+day); if(year<2001)
{
alert("Year must be greater than 2001!");
return false;
}
if(month<0||month>12)
{
alert("Month must range from 1 to 12!");
return false;
} if(day<0||day>31)
{
alert("Day must range from 1 to 31!");
return false;
}
else
{
if(month==2)
{
if(isLeapYear(year)&&day>29)
{
alert("In Month 2,Day must range from 1 to 29!");
return false;
} if(!isLeapYear(year)&&day>28)
{
alert("In Month 2,Day must range from 1 to 28!");
return false;
} } if((month==4||month==6||month==9||month==11)&&(day>30))
{
alert("In this Month ,Day must range from 1 to 30!");
return false;
}
} }
else
{
alert("Date isn''t matched with Format!\nDate Format:"+format);
return false;
} return true;
}资源无限,大家共享!! ^_^
{
var lthdatestr
if (datestr != "")
lthdatestr= datestr.length ;
else
lthdatestr=0;
var tmpy="";
var tmpm="";
var tmpd="";
var status;
status=0;
if ( lthdatestr== 0 )
return false;
for (i=0;i<lthdatestr;i++)
{
if (datestr.charAt(i)== '-'||datestr.charAt(i)=='.'||datestr.charAt(i)=='/')
{
status++;
}
if (status>2)
{
return false;
}
if ((status==0) && (datestr.charAt(i)!='-'&&datestr.charAt(i)!='.'&&datestr.charAt(i)!='/'))
{
tmpy=tmpy+datestr.charAt(i)
}
if ((status==1) && (datestr.charAt(i)!='-'&&datestr.charAt(i)!='.'&&datestr.charAt(i)!='/'))
{
tmpm=tmpm+datestr.charAt(i)
}
if ((status==2) && (datestr.charAt(i)!='-'&&datestr.charAt(i)!='.'&&datestr.charAt(i)!='/'))
{
tmpd=tmpd+datestr.charAt(i)
}
}
year=new String (tmpy);
month=new String (tmpm);
day=new String (tmpd)
if ((tmpy.length!=4) || (tmpm.length>2) || (tmpd.length>2))
{
return false;
}
if ((year<1900) || (year>3000) )
{
return false;
}
if (!((1<=month) && (12>=month) && (31>=day) && (1<=day)) )
{
return false;
}
if (!((year % 4)==0) && (month==2) && (day==29))
{
return false;
}
if ((month<=7) && ((month % 2)==0) && (day>=31))
{
return false;
}
if ((month>=8) && ((month % 2)==1) && (day>=31))
{
return false;
}
if ((month==2) && (day==30))
{
return false;
}
return true;
}