以说明:

//出自http://www.cnblogs.com/ahjesus
尊重作者辛苦劳动成果,转载请注明出处,谢谢!

var idCard = new IdCard();
var msg = idCard.Fill(“430102190001010883”).Check(true);//true
开启检查
console.log(msg);//一下是msg的信息
用于解析15和18各身份证,成功返回
{

Error
 null
 
birthday
 “1900-01-01”
 
city
 “长沙市”
 
county
 “芙蓉区”
 
gender
 “女”
 
idCard
 “430102190001010883”
 
province
 “湖南省”

}

倘Error不为null 则为获得失败 数据格式如下
{
 
Error
 Object { errMsg=”获取区域代码失败”}
 
errMsg
 “获取区域代码失败”
 
birthday
 undefined
 
city
 undefined
 
county
 undefined
 
gender
 undefined
 
idCard
 “430102190001010883”
 
province
 undefined

}

 

目录结构说明:

json 1

areaCode.json    数据源
areaCodeNew.json  采集来之新数据,数据出自http://www.shurufa8.cn/shenfenzheng.html
IdCardIndex.htm    用于解析areaCodeNew.json的页面
my.js        
用于解析areaCodeNew.json的js,配合IdCardIndex.htm使用,定期取得新数据更新areaCodeNew.json重新分析后替换areaCode.json的数据
index.js        解析身份证的js

源码:
  IdCardIndex.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="../../Scripts/Common/jquery-1.8.3.min.js" type="text/javascript"></script>
    <script src="../../Scripts/Common/_CustomJSLibrary.js" type="text/javascript"></script>
    <!--<script src="my.js" type="text/javascript"></script>-->
    <script src="index.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>

  my.js:

$(document).ready(function () {
    var ac = "";

    $.ajax({
        url: "/Plugins/IdCard/areaCodeNew.json",
        type: 'GET',
        timeout: 2000,
        async: false,
        error: function (json) {
            FillResult("获取区域代码失败");
        },//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢!
        success: function (json) {
            ac = json.toJson();
        }
    });

    var array = new Array();
    getT(ac, array);
    function getT(obj, array) {
        if (typeof (obj) == "object") {
            for (var i in obj) {
                getT(obj[i], array);
            }
        }
        else {
            if (!isNaN(obj)) {
                var i = 0;
                while (obj.substr(obj.length - 1, 1) < 1 && i < 10) {
                    obj = obj.substr(0, obj.length - 1);
                    i++;
                }
            }
//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢!
            array.push(obj);
        }
    }

    var newArray = new Array();
    for (var i =0;i< array.length; i++ ) {
        if ((i+1) % 2 == 0) {
            newArray.push(array[i - 1] + "':'" + array[i]);
        }
    }
    var json = "{'" + newArray.join("','") + "'}";
    $("body").text(json);

    function count(obj) {
        var i = 0;
        for (var o in obj) {
            i++;
        }
        return i;
    }
})

  index.js:

function IdCard() {
    this.idCard = "";
};
IdCard.prototype.Fill = function (idCard) {
    this.idCard = idCard;

    function Next(idCard) {//下一步,约束Fill调用后才能调用Check
        this.idCard = idCard;
    };
    Next.prototype.Check = function (enabled) {
        if (!enabled) {
            return null;
        }
        var idCard = this.idCard;

        var resultWraper = {//结果包
            Result: function () {//结果类,
                this.Error = null;
                this.province = "";
                this.city = "";
                this.county = "";
                this.idCard = "";
                this.birthday = "";
                this.gender = "";
            },
            result: null, //结果类的实例
            Error: function () {
                this.errMsg = "";
            }
        };

        function FillResult(errMsg, province, city, county, birthday, gender) {
            var result = new resultWraper.Result();
            if (errMsg != null) {
                var err = new resultWraper.Error();
                err.errMsg = errMsg;
                result.Error = err;
            }
            result.province = province;
            result.city = city;
            result.county = county;
            result.idCard = idCard;
            result.birthday = birthday;
            result.gender = gender;
            resultWraper.result = result;
            return result;
        }

        var InitWraper = {
            AreaCode: (function () {//返回区域编码对应地区的JSON
                var ac = {};
                $.ajax({
                    url: "/Plugins/IdCard/areaCode.json",
                    type: 'GET',
                    timeout: 2000,
                    async: false,
                    error: function (json) {
                        FillResult("获取区域代码失败");
                    },
                    success: function (json) {
                        ac = json.toJson();
                    }
                });
                return ac;
            })(),
            ValidId: (function (pId) {
                var arrVerifyCode = [1, 0, "x", 9, 8, 7, 6, 5, 4, 3, 2];
                var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
                var Checker = [1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1];
                if (pId.length != 15 && pId.length != 18) {
                    FillResult("身份证号共有 15 码或18位");
                } else {
                    var Ai = pId.length == 18 ? pId.substring(0, 17) : pId.slice(0, 6) + "19" + pId.slice(6, 16);
                    if (!/^\d+$/.test(Ai)) {
                        FillResult("身份证除最后一位外,必须为数字!");
                    } else {
                        var yyyy = Ai.slice(6, 10), mm = Ai.slice(10, 12) - 1, dd = Ai.slice(12, 14);
                        var d = new Date(yyyy, mm, dd), year = d.getFullYear(), mon = d.getMonth(), day = d.getDate(), now = new Date();
                        if (year != yyyy || mon != mm || day != dd || d > now || now.getFullYear() - year > 140) {
                            FillResult("身份证输入错误!");
                        } else {
                            for (var i = 0, ret = 0; i < 17; i++) {
                                ret += Ai.charAt(i) * Wi[i];
                            };
                            Ai += arrVerifyCode[ret %= 11];
                            if (pId.length == 18 && pId.toLowerCase() != Ai) {
                                FillResult("身份证输入错误!");
                            }
                        }
                    }
                }
            })(this.idCard)
        };
//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢!
        var funcWraper = {
            province: function () {
                var str = InitWraper.AreaCode[idCard.slice(0, 2)] || "";
                return str;
            },
            city: function () {
                var _province = this.province();
                var str = InitWraper.AreaCode[idCard.slice(0, 4)] || "";
                if (_province != "") {
                    str = str.replace(_province, "");
                }
                return str;
            },
            county: function () {
                var _province = this.province();
                var _city = this.city();
                var str = InitWraper.AreaCode[idCard.slice(0, 6)] || "";
                if (_province != "") {
                    str = str.replace(_province, "");
                    str = str.replace(_city, "");
                }
                return str;
            },
            birthday: function () {
                var tmpStr = "";
                var idDate = "";
                var tmpInt = 0;
                var strReturn = "";
                if (idCard.length == 15) {
                    tmpStr = idCard.substring(6, 12);
                    tmpStr = "19" + tmpStr;
                    tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6);

                }
                else {
                    tmpStr = idCard.substring(6, 14);
                    tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6);

                }
                return tmpStr;
            },
            gender: function () {
                var rst;
                if (idCard.slice(14, 17) % 2 != 0) {
                    rst = "男";
                } else {
                    rst = "女";
                }
                return rst;
            }
        };

        if (resultWraper.result == null) {
            var _province = funcWraper.province();
            var _city = funcWraper.city();
            var _county = funcWraper.county();
            var _birthday = funcWraper.birthday();
            var _gender = funcWraper.gender();
            FillResult(null, _province, _city, _county, _birthday, _gender);
        };
        return resultWraper.result;
    }
//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢!
    return new Next(idCard);
};

 

  
 

相关文章

网站地图xml地图