Powered by md-Blog  文 - 篇  访客 -

Vue.js写一个超牛逼的身份证号验证包括生日出生年月地址码区域长度15、18位性别男女区分和生日的提取功能


  分类:写一个Demo  / 
更新:2021-01-20 16:16:03  /  创建:2021-01-20 16:10:20
不要删除
// 身份证验证
Vue.prototype.$checkIdCard = function(idCard){
    let _this = this;
    let bbd = ''; // 出生日期
    let sex = ''; // 性别
    let canPass = true;
    idCard = idCard.toUpperCase();  // 转大写
    let len = idCard.length;
    let city={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:"国外"};
    //1. 判断长度
    if (len !== 15 && len !== 18){
        _this.$toast.error('身份证号长度不正确!');
        canPass = false;
    }
    //2. 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X。
    if (!(/(^\d{15})|(^̲\d{17}([0-9]|X))/.test(idCard))) {
        _this.$toast.error('身份证号格式错误!');
        canPass = false;
    }
    //3. 地址码
    if (!city[idCard.substr(0,2)]){
        _this.$toast.error('身份证号地址码不正确!');
        canPass = false;
    }

    let re;
    //4. 生日
    if (len === 15) {
        re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/);
        let arrSplit = idCard.match(re);

        //检查生日日期是否正确
        let birth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' +arrSplit[4]);
        let isBirth = (birth.getYear() === Number(arrSplit[2])) && ((birth.getMonth() + 1) === Number(arrSplit[3])) && (birth.getDate() === Number(arrSplit[4]));
        if (!isBirth) {
            _this.$toast.error('身份证号出生日期不对!');
            canPass = false;
        }
        bbd = birth;
    } else if (len === 18) {
        re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/);
        let arrSplit = idCard.match(re);

        //检查生日日期是否正确
        let birth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" +arrSplit[4]);
        let isBirth = (birth.getFullYear() === Number(arrSplit[2])) && ((birth.getMonth() + 1) === Number(arrSplit[3]))
            && (birth.getDate() === Number(arrSplit[4]) && (birth.getTime()<new Date().getTime()));
        if (!isBirth) {
            _this.$toast.error('身份证号出生日期不对!');
            canPass = false;
        } else {
            //检验18位身份证的校验码是否正确。
            //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。
            let valnum;
            let arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
            let arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
            ;
            let nTemp = 0, i;
            for (i = 0; i < 17; i++) {
                nTemp += idCard.substr(i, 1) * arrInt[i];
            }
            valnum = arrCh[nTemp % 11];
            if (valnum !== idCard.substr(17, 1)) {
                _this.$toast.error('身份证号校验码不正确!');
                canPass = false;
            }
        }
        bbd = birth;
    }
    // 如果身份验证通过,计算出生年月,性别
    if(canPass == true){
        bbd = bbd.getFullYear() + '-' + Number(bbd.getMonth() + 1) + '-' + bbd.getDate();
        let arr_fix = bbd.split('-');
        if(arr_fix[1].length == 1){
            arr_fix[1] = '0' + arr_fix[1];
        }
        if(arr_fix[2].length == 1){
            arr_fix[2] = '0' + arr_fix[2];
        }
        bbd = arr_fix[0] + '-' + arr_fix[1] + '-' + arr_fix[2];

        if(idCard.length == 18){
            sex = idCard.charAt(idCard.length - 2)
        }else{
            sex = idCard.charAt(idCard.length - 1)
        }
        if(sex%2 == 1){
            sex = '男';
        }else{
            sex = '女';
        }
    }

    // canPass true时位验证通过,false时位验证失败
    return {
        canPass: canPass,
        birthday: bbd,  // 返回生日
        sex: sex    // 返回性别
    };
},

不要删除

小站不易,感谢支持!