http://www.cnblogs.com/jiandanfy/articles/1061248.html
<body test="green" ><center><br><br><br><br><br><br><br><br> <script language="javascript"> //日期 初步验证 function isValidDate(day, month, year) { if (month == 2) { var leap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); if (day>29 || (day == 29 && !leap)) { return false; } } return true; } function checkId(pId){ var arrVerifyCode = [1,0,"x",9,8,7,6,5,4,3,2]; var Wi = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]; var Checker = [1,9,8,7,6,5,4,3,2,1,1]; if(pId.length != 15 && pId.length != 18) return "身份证号共有 15 码或18位"; var Ai=pId.length==18?pId.substring(0,17):pId.slice(0,6)+"19"+pId.slice(6,16); if (!/^\d+$/.test(Ai)) return "身份证除最后一位外,必须为数字!"; var yyyy=Ai.slice(6,10),mm=Ai.slice(10,12)-1,dd=Ai.slice(12,14); var d=new Date(yyyy,mm,dd),year=d.getFullYear(),mon=d.getMonth(),day=d.getDate(),now=new Date(); if (year!=yyyy || mon!=mm || day!=dd || d>now || now.getFullYear()-year>110 ||!isValidDate(dd, mm, yyyy) ) return "身份证输入错误!"; for(var i=0,ret=0;i<17;i++) ret+=Ai.charAt(i)*Wi[i]; Ai+=arrVerifyCode[ret %=11]; return pId.length ==18 && pId.toLowerCase() != Ai?"身份证输入错误!":Ai; }; function getInfo(id){ //根据身份证取 省份,生日,性别 id=checkId(id) var fid=id.substring(0,16),lid=id.substring(17); if (isNaN(fid)|| ( isNaN(lid) && (lid!="x") )) return "错误的身份证号码" var id=String(id),sex=id.slice(14,17)%2?"男":"女" prov=areaCode[id.slice(0,6)] || areaCode[id.slice(0,4)] || areaCode[id.slice(0,2)] || "无法确定" var birthday=(new Date(id.slice(6,10),id.slice(10,12)-1,id.slice(12,14))).toLocaleDateString() return [prov,birthday,sex] } function test(){ var cid = document.getElementById("cid"); alert(getInfo(cid.value)) ; } </script> <form> <input type="text" id="cid"> <input type="button" onclick="test()" value="检查输入的身份证号是否正确"> </form> <center><body>