<html>
<script>
function RunNian(The_Year)
{
if ((The_Year%400==0) || ((The_Year%4==0) && (The_Year%100!=0)))
return true;
else
return false;
}function showme()
{
if(window.inputYear.value < 1900)
{
alert("年份输入错误!");
return false;
}
if((window.inputMonth.value < 1) || (window.inputMonth.value > 12))
{
alert("月份输入错误!");
return false;
}
if( window.inputMonth.value == 2)
{
if ( !RunNian(window.inputYear.value) )
{
if(window.inputDay.value > 28)
{
alert("输入日数不能大于28!");
return false;
}
}
if(window.inputDay.value > 29)
{
alert("输入日数不能大于29!");
return false;
}
}
if((window.inputDay.value < 1) || (window.inputDay.value > 31))
{
alert("日期输入错误!");
return false;
}
</script><body>
<input name="inputYear" type="text">
<input name="inputMonth" type="text">
<input name="inputDay" type="text">
<input type="button" value="showDate" onclick = "showme();">
</body>
</html>
<script>
function RunNian(The_Year)
{
if ((The_Year%400==0) || ((The_Year%4==0) && (The_Year%100!=0)))
return true;
else
return false;
}function showme()
{
if(window.inputYear.value < 1900)
{
alert("年份输入错误!");
return false;
}
if((window.inputMonth.value < 1) || (window.inputMonth.value > 12))
{
alert("月份输入错误!");
return false;
}
if( window.inputMonth.value == 2)
{
if ( !RunNian(window.inputYear.value) )
{
if(window.inputDay.value > 28)
{
alert("输入日数不能大于28!");
return false;
}
}
if(window.inputDay.value > 29)
{
alert("输入日数不能大于29!");
return false;
}
}
if((window.inputDay.value < 1) || (window.inputDay.value > 31))
{
alert("日期输入错误!");
return false;
}
</script><body>
<input name="inputYear" type="text">
<input name="inputMonth" type="text">
<input name="inputDay" type="text">
<input type="button" value="showDate" onclick = "showme();">
</body>
</html>
function testDate(sDate){
execScript('t=IsDate("'+sDate+'"','vbscript')
if(t)return true;
else return false
}alert(testDate('2003-02-03'));alert(testDate('2003-02-03 12:34'))
var regstr=/^\d{4}-(\d{1}|11|12)-([1-2]?\d{1}|3[0-1])$/;
return regstr.test(str);
这一句能匹配10月份吗
/// <summary>
/// 校验具有value属性的控件的value年月日字符串是否合法(Date:YYYY-MM-DD),不合
//法就给出提示信息并使其获得焦点。
/// </summary>
/// <param name="date">待较验的年月日字符串</param>
/// <returns>True,合法;False:不合法,并返回错误信息:message</returns>
/// 使用方法:ID指控件的ID标识;onBlur:失去焦点
/// onBlur="javascript:IsValidDate(ID);"
function IsValidDate(ID)
{
var sDate=ID.value.replace(/(^\s+|\s+$)/g,""); //去两边空格;
if(sDate=="")
return true;
//如果格式满足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为""
//数据库中,合法日期可以是:YYYY-MM/DD(2003-3/21),数据库会自动转换为YYYY-MM-DD格式
var s = sDate.replace(/[\d]{4,4}[\-/]{1}[\d]{1,2}[\-/]{1}[\d]{1,2}/g,"");
if (s=="") //说明格式满足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D
{
var t=new Date(sDate.replace(/\-/g,"/"));
var ar = sDate.split(/[-/ :]/);
var k = false;
if(ar[0] != t.getYear() || ar[1] != t.getMonth()+1 || ar[2] != t.getDate())
k = true;
if(k)
{
alert("错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。");
ID.focus();
return false;
}
}
else
{
alert("错误的日期格式!格式为:YYYY-MM-DD或YYYY/MM/DD。注意闰年。");
ID.focus();
return false;
}
return true;
}