码迷,mamicode.com
首页 > 其他好文 > 详细

18位身份证号验证及信息获取

时间:2014-07-19 16:24:38      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:blog   java   io   for   cti   re   

止乎于分享!

IDCode18 = {
    validate: function (value) {
        if (value.length != 18) return false;
        var value = value.toLowerCase();
        var sum = 0, v = ‘10x98765432‘, w = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2], a = ‘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 re = value.match(/^(\d{2})\d{4}(\d{4})(\d{2})(\d{2})\d{3}[x\d]$/);
        if (re == null || a.indexOf(re[1]) < 0) return false;
        if (!this._validateDate(re[2], re[3], re[4])) return false;
        for (var i = 0; i < 17; i++) sum += value.charAt(i) * w[i];
        return value.charAt(17) == v.charAt(sum % 11);
    },
    getBirth: function (value, fmt) {
        if (!this.validate(value)) return ‘‘;
        fmt = fmt || ‘yyyy-MM-dd‘;
        var y = value.substr(6, 4), m = value.substr(10, 2), d = value.substr(12, 2);
        return fmt.replace(‘yyyy‘, y).replace(‘MM‘, m).replace(‘M‘, parseInt(m, 10)).replace(‘dd‘, d).replace(‘d‘, parseInt(d, 10));
    },
    getSex: function (value) {
        if (!this.validate(value)) return ‘‘;
        return value.substr(16, 1) % 2 == 0 ? ‘女‘ : ‘男‘;
    },
    _validateDate: function (y, m, d) {
        var dd = new Date(y, parseInt(m, 10) - 1, parseInt(d, 10));
        return dd.getFullYear() == y && dd.getMonth() + 1 == parseInt(m, 10) && dd.getDate() == parseInt(d, 10);
    }
}

18位身份证号验证及信息获取,布布扣,bubuko.com

18位身份证号验证及信息获取

标签:blog   java   io   for   cti   re   

原文地址:http://www.cnblogs.com/njl041x/p/3853928.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!