Back to HomePage

中国身份证验证模块

算法/Algorithm

18位身份证标准在国家质量技术监督局于1999年7月1日实施的GB11643-1999《公民身份号码》中做了明确的规定。 GB11643-1999《公民身份号码》为GB11643-1989《社会保障号码》的修订版,其中指出将原标准名称"社会保障号码"更名为"公民身份号码",另外GB11643-1999《公民身份号码》从实施之日起代替GB11643-1989。GB11643-1999《公民身份号码》主要内容如下:

一、范围
该标准规定了公民身份号码的编码对象、号码的结构和表现形式,使每个编码对象获得一个唯一的、不变的法定号码。

二、编码对象
公民身份号码的编码对象是具有中华人民共和国国籍的公民。

三、号码的结构和表示形式

1、号码的结构
公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。

2、地址码
表示编码对象常住户口所在县(市、旗、区)的行政区划代码,按GB/T2260的规定执行。

3、出生日期码
表示编码对象出生的年、月、日,按GB/T7408的规定执行,年、月、日代码之间不用分隔符。

4、顺序码
表示在同一地址码所标识的区域范围内,对同年、同月、同日出生的人编定的顺序号,顺序码的奇数分配给男性,偶数分配给女性。

5、校验码
(1)十七位数字本体码加权求和公式
S = Sum(Ai * Wi), i = 0, ... , 16 ,先对前17位数字的权求和
Ai:表示第i位置上的身份证号码数字值
Wi:表示第i位置上的加权因子
Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
(2)计算模
Y = mod(S, 11)
(3)通过模得到对应的校验码
Y: 0 1 2 3 4 5 6 7 8 9 10
校验码: 1 0 X 9 8 7 6 5 4 3 2

参考资料:

Example

安装模块可以从命令行
cpan Business::CN::IdentityCard
也可以在此下载


use Business::CN::IdentityCard;

my $id = '11010519491231002X'; # 你所获得的身份证号码
my $idv = new Business::CN::IdentityCard;
if ($idv->validate($id)) { # 调用 validate
    print 'Pass';
    print $idv->gender; # 返回性别,默认为中文的“男”或“女”
    print $idv->gender('EN'); # 返回英文性别: Male|Female
    print $idv->birthday; # 生日,类如: 1975-10-31
    print $idv->province; # 省份,用中文表示
    print $idv->district; # 地区
} else {
    print $idv->err; # 错误类型,比如 BIRTHDAY PROVINCE 等
    print $idv->errstr; # 详细错误信息
}

CHANGES

  1. Adam Kennedy report a bug like this:
    Fayland
    
    I have just filed an RT bug against China::IdentityCard::Validate 
    regarding it's name, which is unusual and quite out of place.
    
    Countries to not have top level CPAN namespaces.
    
    Must number modules like yours currently exist in the Business:: 
    namespace, which you result in your module being better named.
    
    Business::CN::IdentityCard
    
    ....
    
    So I changed the module name from 'China::IdentityCard::Validate' to 'Business::CN::IdentityCard'
  2. Joe Jiang reported me a bug, that the regex for birthday was wrong. I updated the module, thanks for joe's help.
  3. chunzi 提供了一个加强版的原型。此版本是在那基础上修改的。Thanks chunzi.

Copyright 2004-2005 All Rights Reserved.