if(idTypeIsChecked.checked == true){//如果选中的是二代身份证 var birthDateValue = document.getElementById('idNumber').value; var birthYear = birthDateValue.substr(6, 4); //截取年 var birthMonth = birthDateValue.substr(10, 2); //截取月 var birthDay = birthDateValue.substr(12, 2); //截取日 var date = new Date(); var year = date.getFullYear();//获取系统当前年 var month = date.getMonth()+1;//获取系统当前月并+1 var today = date.getDate();//获取系统当前日 if(birthDateValue && birthDateValue != "" && birthDateValue.length == 18){ if(birthYear > year || (year - birthYear) > 150 || birthMonth > 12 || birthDay > 31){//如果自动生成了非法的出生日期,出生日期将设置为当前系统日期 birthYear = year; if(month < 10){ month = "0"+month;//如果小于0则前面补0 } birthMonth = month; if(today < 10){ today = "0"+today;//如果小于0则前面补0 } birthDay = today; } document.getElementById('birthDate').value = birthYear+"-"+birthMonth+"-"+birthDay; } }