欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js检查身份证号是否正确

程序员文章站 2023-02-20 23:11:51
转的,比较完善的验证身份证号的代码 ......

转的,比较完善的验证身份证号的代码

 1 /*
 2 check(id)验证身份证号码
 3 返回值:0 : "是正确的身份证号"
 4         1 : "身份证校验不符合求和模11=1这个等式",
 5         2 : "身份证最后一位校验位有误",
 6         3 : "身份证位数不正确",
 7         4 : "年份错误",
 8         5 : "月份错误",
 9         6 : "日错误"
10         
11 */
12 
13 function check(id) {
14     var ck = new idcard();
15     ck.check(id);
16     return ck.errnum;
17 }
18 
19 function idcard(id) {
20     var me = this;
21     this.checkcode = new array(1, 0, 'x', 9, 8, 7, 6, 5, 4, 3, 2);
22     this.id;
23     this.errnum = 0;
24     this.err = {
25         1 : "身份证校验不符合求和模11=1这个等式",
26         2 : "身份证最后一位校验位有误",
27         3 : "身份证位数不正确",
28         4 : "年份错误",
29         5 : "月份错误",
30         6 : "日错误"
31     };
32     this.err_msg = function() {
33         return (0 !== this.errnum) ? this.err[this.errnum] : null;
34     };
35     this.check = function(id) {
36         this.errnum = 0;
37         this.id = id;
38         if (cardvalidate()) {
39             if (!aicheck()) this.errnum = 1;
40             if (!lastbytecheck()) this.errnum = 2;
41             return (0 === this.errnum) ? true: false;
42         } else {
43             return false;
44         }
45     };
46     function getnum(str) {
47         var re = /^[0]*([0-9]*)$/;
48         str.match(re);
49         return parseint(regexp.$1,10);
50     }
51     //对身份证位数,出生日期,初步验证
52     function cardvalidate() {
53         var re = /^[0-9x]{15,18}$/i;
54         if (!re.test(me.id)) {
55             me.errnum = 7;
56             return false;
57         }
58         if (15 == me.id.length || 18 == me.id.length) {
59             var reallen = 0;
60             if (18 == me.id.length) reallen = 2;
61             var year = parseint(me.id.substr(6, 2 + reallen),10);
62             var month = getnum(me.id.substr(8 + reallen, 2));
63             var day = parseint(me.id.substr(10 + reallen, 2),10);
64             if (4 == year.length) if (year < 1900 || year > 2050) me.errnum = 4;
65             if (month < 1 || month > 12) me.errnum = 5;
66             if (day < 1 || day > 31) me.errnum = 6;
67             return 0 === me.errnum;
68         } else {
69             me.errnum = 3;
70             return false;
71         }
72     }
73     //总和模11应该等于1
74     var aicheck = function() {
75         var sum = 0;
76         for (var i = 1; i <= me.id.length; i++) {
77             var ai = me.id.substr(me.id.length - i, 1);
78             ai = (ai.tolowercase() == 'x') ? 10 : ai;
79             var wi = math.pow(2, i - 1) % 11;
80             sum += parseint(ai,10) * wi;
81         }
82         return 1 == sum % 11;
83     };
84     //最后一位校验,这个可以和aicheck合在一起,减少一次循环
85     var lastbytecheck = function() {
86         var sum = 0;
87         for (var i = 2; i <= me.id.length; i++) {
88             var ai = me.id.substr(me.id.length - i, 1);
89             ai = (ai.tolowercase() == 'x') ? 10 : ai;
90             var wi = math.pow(2, i - 1) % 11;
91             sum += parseint(ai,10) * wi;
92         }
93         var checkcodeindex = sum % 11;
94         var c = me.id.substr(me.id.length - 1, 1);
95         //alert(c+"=="+me.checkcode[checkcodeindex]);
96         return c == me.checkcode[checkcodeindex];
97     };
98 }