身份证号码中日期码验证测试程序
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>三十客-身份证号码中日期码验证测试程序</title> </head> <body> <div id="main"></div> <script> console.log = function(val) { document.getElementById("main").innerHTML += val +"<br />"; } var checkDate = function (val) { var pattern = /^(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)$/; if(pattern.test(val)) { var year = val.substring(0, 4); var month = val.substring(4, 6); var date = val.substring(6, 8); var date2 = new Date(year+"-"+month+"-"+date); if(date2 && date2.getMonth() == (parseInt(month) - 1)) { return true; } } return false; } //输出 true console.log(checkDate("20180212")); //输出 false 2月没有31日 console.log(checkDate("20180231")); </script> </body> </html>