[导读] var idcardvalid = { 身份证检查isidcard : function(_id) {var returnobj = new object();var id = $ trim(_id touppercase());var idarray = this _splitid(id);if (idarray length > 0)
var idcardvalid = {//身份证检查
isidcard : function(_id) {
var returnobj = new object();
var id = $.trim(_id.touppercase());
var idarray = this._splitid(id);
if (idarray.length > 0) {
if (id.length != 18) {
returnobj.status = false;
returnobj.info = " 身份证号码必须是18位";
return returnobj;
}
var checksum = id.substring(17, 18);
var sum = 0;
for ( var i = 1; i <= idarray.length; i++) {
var ai = idarray[i - 1];
var wi = this._getweightingfactor(i);
if (wi != -1) {
sum += ai * wi;
}
}
var mod = sum % 11;
if (this._getchecksum(mod) == checksum) {
returnobj.status = true;
return returnobj;
} else {
returnobj.status = false;
returnobj.info = " 身份证号码输入错误";
return returnobj;
}
} else {
returnobj.status = false;
returnobj.info = " 身份证号码格式错误";
return returnobj;
}
},
_splitid : function(_id) {
var len = _id.length;
var array = new array();
for ( var i = 0; i < len - 1; i++) {
array.push(_id.charat(i));
}
return array;
},
_getweightingfactor : function(_index) {
switch (_index) {
case 1:
return 7;
case 2:
return 9;
case 3:
return 10;
case 4:
return 5;
case 5:
return 8;
case 6:
return 4;
case 7:
return 2;
case 8:
return 1;
case 9:
return 6;
case 10:
return 3;
case 11:
return 7;
case 12:
return 9;
case 13:
return 10;
case 14:
return 5;
case 15:
return 8;
case 16:
return 4;
case 17:
return 2;
default:
return -1;
}
},
_getchecksum : function(_mod) {
switch (_mod) {
case 0:
return 1;
case 1:
return 0;
case 2:
return "x";
case 3:
return 9;
case 4:
return 8;
case 5:
return 7;
case 6:
return 6;
case 7:
return 5;
case 8:
return 4;
case 9:
return 3;
case 10:
return 2;
default:
return -1;
}
}
};
以上就是javascript身份证真伪校验方法的详细内容。