<script language="JavaScript">
<!--function CheckDate(strDate){
var reg=/^(\d{4})([-])(\d{2})([-])(\d{2})/;
if(!reg.test(strDate)){
alert("日期格式不正确!\n正确格式为:2004-01-01");
return false;
}
var ss=strDate.split("-");
var year=ss[0];
var month=ss[1];
var date=ss[2];
if(!checkYear(year)){return false;}
if(!checkMonth(month)){return false;}
if(!checkDate(year,month,date)){return false;}
return true;
}
function checkYear(year){
if(isNaN(parseInt(year))){alert("年份输入有误,请重新输入!"); return false;}
else if(parseInt(year)<1950 || parseInt(year) >2050){ alert("年份应该在1950-2050之间!"); return false}
else return true;
}
function checkMonth(month){
if(isNaN(parseInt(month))){alert("月份输入有误,请重新输入!"); return false;}
else if(parseInt(month)<1 || parseInt(month) >12){ alert("月份应该在1-12之间!"); return false}
else return true;
}
function checkDate(year,month,date){
var daysOfMonth=CalDays(parseInt(year),parseInt(month));
if(isNaN(parseInt(date))){alert("日期输入有误,请重新输入!"); return false;}
else if(parseInt(date)<0||parseInt(date)>daysOfMonth){ alert("日期应该在1-"+daysOfMonth+"之间!"); return false;}
else return true;
}
function CalDays(year,month){
var days=0;
switch(month){
case 1: case 3: case 5: case 7: case 8: case 10: case 12: days=31;break;
case 4: case 6: case 9: case 11: days=30;break;
case 2: if(isLeapYear(year)) days=29;
else days=28;
break;
}
return days;
}
function isLeapYear(year){
if((year %4==0 && year %100!=0) || (year %400==0)) return true;
else return false;
}
alert(CheckDate("2005-02-29"));
//-->
</script>只有验证日期的,后边的时间可以自己加
<!--function CheckDate(strDate){
var reg=/^(\d{4})([-])(\d{2})([-])(\d{2})/;
if(!reg.test(strDate)){
alert("日期格式不正确!\n正确格式为:2004-01-01");
return false;
}
var ss=strDate.split("-");
var year=ss[0];
var month=ss[1];
var date=ss[2];
if(!checkYear(year)){return false;}
if(!checkMonth(month)){return false;}
if(!checkDate(year,month,date)){return false;}
return true;
}
function checkYear(year){
if(isNaN(parseInt(year))){alert("年份输入有误,请重新输入!"); return false;}
else if(parseInt(year)<1950 || parseInt(year) >2050){ alert("年份应该在1950-2050之间!"); return false}
else return true;
}
function checkMonth(month){
if(isNaN(parseInt(month))){alert("月份输入有误,请重新输入!"); return false;}
else if(parseInt(month)<1 || parseInt(month) >12){ alert("月份应该在1-12之间!"); return false}
else return true;
}
function checkDate(year,month,date){
var daysOfMonth=CalDays(parseInt(year),parseInt(month));
if(isNaN(parseInt(date))){alert("日期输入有误,请重新输入!"); return false;}
else if(parseInt(date)<0||parseInt(date)>daysOfMonth){ alert("日期应该在1-"+daysOfMonth+"之间!"); return false;}
else return true;
}
function CalDays(year,month){
var days=0;
switch(month){
case 1: case 3: case 5: case 7: case 8: case 10: case 12: days=31;break;
case 4: case 6: case 9: case 11: days=30;break;
case 2: if(isLeapYear(year)) days=29;
else days=28;
break;
}
return days;
}
function isLeapYear(year){
if((year %4==0 && year %100!=0) || (year %400==0)) return true;
else return false;
}
alert(CheckDate("2005-02-29"));
//-->
</script>只有验证日期的,后边的时间可以自己加
解决方案 »
- 求一个正则表达式
- document.referrer的问题
- 极高分求助:同一 frameset里,A页面可以调用B页面的javascript函数吗?可以控制B页面的定时器吗?如何处理呢?多谢
- 弹出窗口全屏幕的问题!
- 求一有时/分/秒的时间控件
- 能用javascript把IE中的打印设置里页眉和页脚清除掉???
- 关于FORM的一个问题
- javascript的confirm对话框,点击“确定”后,触发form中的一个按钮(submit)的onclick事件
- netscape中返回到前一叶面都可以有哪些方法?
- 用两个按钮来控制input type="text"中文字的大小和颜色 谁能帮帮忙?
- 关于表格的一些应用问题
- 在单击id="tbl"中任意一格或多格时,如何将id="mtbl"中的所有格(因单击id="tbl"中的onclick产生的背景色)全部清除掉?下面的例子只能清除
yyyy-(M)M-(d)d 格式 4位年,1或2位月(日)
*/
function validDateFormat(dateStr){
if(dateStr.length==0)
return false ;
var reg = /^(\d{4})(\/|-)(\d{1,2})\2(\d{1,2})$/
var arr = reg.exec(dateStr);
if (arr == null)
return false;
var date = new Date(arr[1],arr[3]-1,arr[4]);
return date.getFullYear()==arr[1] && date.getMonth()==(arr[3]-1) && date.getDate()==arr[4];
}