js 判斷身份證好是否合法

function cidInfo(sId){ 
var iSum=0 
var info="" 
//if(!/^\d{17}(\d|x)$/i.test(sId))return false; 
sId=sId.replace(/x$/i,"a");
if (aCity[parseInt(sId.substr(0, 2))] == null) return "身份證格式不正確"; //"Error:非法地區"; 
sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2)); 
var d=new Date(sBirthday.replace(/-/g,"/"))
if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) return "身份證格式不正確"; //"Error:非法生日"; 
for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11)
if (iSum % 11 != 1) return "身份證格式不正確"; //"Error:非法證號";
return "";
//return aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女") 
} 
相關文章
相關標籤/搜索