1 判断日期是否正确。
//日付タイプチェック
function isStrDate(strDateObj) {
var strDate = strDateObj.value;
var wDate;
var adays;
if (strDate == '') {
return true;
}
var aMatch1 = /^/d{8}$/;
var aMatch2 = /^(/d{4})(-){1}(/d{2})(-){1}(/d{2})|(/d{4})(//){1}(/d{2})(//){1}(/d{2})$/;
if (!aMatch1.test(strDate) && !aMatch2.test(strDate)){
alert(errors_format_data);
strDateObj.focus();
return false;
}
try{
if (strDate.indexOf('-') != -1) {
strDate = strDate.replace(new RegExp('-', "g"),'/');
} else if (strDate.indexOf('/') == -1) {
strDate = strDate.substr(0,4)+'/'+strDate.substr(4,2)+'/'+strDate.substr(6,2);
}
wDate = new Date(strDate);// 这是为从设日期,如:strDate = 2007/12/35,那么 wDate = 2008/01/04。
}catch(e){
alert(errors_format_data);
strDateObj.focus();
return false;
}
adays = strDate.split('/');
if (adays[0].substr(0,2) == '19') { // 这是因为当年份< 2000 是,wDate.getYear() 的结果是四位年份的后两位, 如,1987,则wDate.getYear() = 87 。
adays[0] = parseInt(adays[0].substr(2,2));
}
if(wDate.getYear() != adays[0] || wDate.getMonth()+1 != adays[1] || wDate.getDate() != adays[2]){
strDateObj.focus();
return false ;
}
strDateObj.value = strDate;
return true;
}