/*
* 日期时间合法性验证函数,合法的格式包括(yyyy-mm-dd hh:mm)
* param str String 待验证的字符串
* return 如果合法,返回true
*/
function valiDateTime(str){
var reg = /^(\d+)-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2})$/;
var r = str.match(reg);
if(r==null)return false;
r[2]=r[2]-1;
var d= new Date(r[1], r[2],r[3], r[4],r[5]);
if(d.getFullYear()!=r[1])return false;
if(d.getMonth()!=r[2])return false;
if(d.getDate()!=r[3])return false;
if(d.getHours()!=r[4])return false;
if(d.getMinutes()!=r[5])return false;
return true;
}
/*
* 日期时间合法性验证函数,合法的格式包括(yyyy-mm-dd hh:mm:ss)
* param str String 待验证的字符串
* return 如果合法,返回true
*/
function valiDateTimes(str){
var reg = /^(\d+)-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var r = str.match(reg);
if(r==null)return false;
r[2]=r[2]-1;
var d= new Date(r[1], r[2],r[3], r[4],r[5], r[6]);
if(d.getFullYear()!=r[1])etMonth()!=r[2])return false;
if(d.getDate()!=r[3])return false;
if(d.getHours()!=r[4])return false;
if(d.getMinutes()!=r[5])return false;
if(d.getSeconds()!=r[6])return false;
return true;
}
jQuery.validator 验证扩展
jQuery.validator.addMethod("isDate",function(value) {
var reg = /^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var r = value.match(reg);
if(r == null) return false;
var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
return (d.getFullYear() == r[1] && (d.getMonth()+1)==r[3] && d.getDate()==r[4] && d.getHours()==r[5] && d.getMinutes()==r[6] && d.getSeconds()==r[7]);
}, "时间格式错误");