JS 分别验证年、月、日正则式

[复制链接]
查看11 | 回复1 | 2009-12-14 22:26:00 | 显示全部楼层 |阅读模式
正则表达式偶不太会 给你这个参考参考欧文一直都用这个的嘻嘻 function CheckDate(strDate){
//var strDate = document.getElementById("date_hour").value;
var reg=/^(\d{4})(\d{2})(\d{2})$/;
if(!reg.test(strDate)){
alert("日期格式不正确!\n正确格式为:20040101");
return false;
}
//var ss=strDate.split("/");
//var year=ss[0];
//var month=ss[1];
//var date=ss[2];
var year=strDate.substring(0,4);
var month=strDate.substring(4,6);
var date=strDate.substring(6,8);
//alert(year+month+date);
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)2050)
{
alert("年份应该在1950-2050之间!");
return false;
}
else return true;} function checkMonth(month){if(isNaN(parseInt(month,10))){alert("月份输入有误,请重新输入!"); return false;}
else if(parseInt(month,10)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)daysOfMonth){ alert("日期应该在1-"+daysOfMonth+"之间!"); return false;}
else return true;} function CalDays(year,month){var date= new Date(year,month,0);return date.getDate();} function isLeapYear(year){if((year %4==0 && year %100!=0) || (year %400==0)) return true;else return false;}
回复

使用道具 举报

千问 | 2009-12-14 22:26:00 | 显示全部楼层
正则不是万能的,虽然能用正则处理,但是为什么不转换类型为日期时间,看异常信息呢?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行