//歡迎驗證、給予指正錯誤,從好多地方查來的資料修改的...已用本人身份證驗證... 由于以前修改的問題無法驗證帶 X 的身份證号碼,已修複!
//為值添加0
function Append_zore(temp)
{
if(temp<10)
{
return "0"+temp;
}
else
{
return temp;
}
}
//身份證号碼驗證
$("#person_id").change(function(){
if($("#person_id").val()!="")
{
//身份證的地區代碼對照
var aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "遼甯", 22: "吉林", 23: "黑龍江", 31: "上海", 32: "江蘇", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山東", 41: "河南", 42: "湖北", 43: "湖南", 44: "廣東", 45: "廣西", 46: "海南", 50: "重慶", 51: "四川", 52: "貴州", 53: "雲南", 54: "西藏", 61: "陝西", 62: "甘肅", 63: "青海", 64: "甯夏", 65: "新疆", 71: "台灣", 81: "香港", 82: "澳門", 91: "國外" };
//獲驗證件号碼
var person_id=$("#person_id").val();
//合法性驗證
var sum = 0;
//出生日期
var birthday;
//驗證長度與格式規範性的正則
var pattern=new RegExp(/(^\d{15}$)|(^\d{17}(\d|x|X)$)/i);
if (pattern.exec(person_id)) {
//驗證身份證的合法性的正則
pattern=new RegExp(/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/);
if(pattern.exec(person_id)) {
//擷取15位證件号中的出生日期并轉位正常日期
birthday = "19"+person_id.substring(6,8)+"-"+person_id.substring(8,10)+"-"+person_id.substring(10,12);
}
else
{
person_id = person_id.replace(/x|X$/i,"a");
//擷取18位證件号中的出生日期
birthday =person_id.substring(6,10)+"-"+person_id.substring(10,12)+"-"+person_id.substring(12,14);
//校驗18位身份證号碼的合法性
for (var i = 17; i >= 0; i--)
{
sum += (Math.pow(2, i) % 11) * parseInt(person_id.charAt(17 - i), 11);
}
if (sum % 11 != 1) {
$(this).addClass("txtRequired");
alert("身份證号碼不符合國定标準,請核對!");
//$("#person_id").val("");
$("#birthday").val("")
return;
}
}
//檢測證件地區的合法性
if (aCity[parseInt(person_id.substring(0, 2))] == null)
{
$(this).addClass("txtRequired");
alert("證件地區未知,請核對!");
//$("#person_id").val("");
$("#birthday").val("");
return;
}
var dateStr = new Date(birthday.replace(/-/g, "/"));
//alert(birthday +":"+(dateStr.getFullYear()+"-"+ Append_zore(dateStr.getMonth()+1)+"-"+ Append_zore(dateStr.getDate())))
if (birthday != (dateStr.getFullYear()+"-"+ Append_zore(dateStr.getMonth()+1)+"-"+ Append_zore(dateStr.getDate()))) {
$(this).addClass("txtRequired");
alert("證件出生日期非法!");
//$("#person_id").val("");
$("#birthday").val("");
return;
}
$(this).removeClass("txtRequired");
$("#birthday").val(birthday);
}
else
{
$(this).addClass("txtRequired");
alert("證件号碼格式非法!");
//$("#person_id").val("");
$("#birthday").val("")
return;
}
}
else
{
alert("請輸入證件号!");
$("#birthday").val("");
}
});