JSt验证时间格式
1. 短时间,形如 (13:04:06)function isTime(str)
{
var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
if (a == null) {alert('输入的参数不是时间格式'); return false;}
if (a>24 || a>60 || a>60)
{
alert("时间格式不对");
return false
}
return true;
}
2. 短日期,形如 (2003-12-05)
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, r-1, r);
return (d.getFullYear()==r&&(d.getMonth()+1)==r&&d.getDate()==r);
}
3. 长时间,形如 (2003-12-05 13:04:06)
function strDateTime(str)
{
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var r = str.match(reg);
if(r==null)return false;
var d= new Date(r, r-1,r,r,r,r);
return (d.getFullYear()==r&&(d.getMonth()+1)==r&&d.getDate()==r&&d.getHours()==r&&d.getMinutes()==r&&d.getSeconds()==r);
}
受教了!这个论坛果然很不错,适合学习
页:
[1]