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

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);
    }
}
posted @ 2014-07-18 17:07  i迷倪  阅读(842)  评论(0编辑  收藏  举报