如何判断身份证是否长期有效(复选框状态)

发布时间 2023-09-01 13:13:40作者: 伟笑
initCheckLongTime() {
            // 计算年月
            let birthYear = this.prpLpayPerson.identifyNumber ? this.prpLpayPerson.identifyNumber.substring(6, 10) : "";
            console.log(birthYear, 'birthYear');
            let nowYear = new Date().getFullYear();
            console.log(nowYear, 'nowYear');
            let year = nowYear - birthYear;
            console.log(year, 'year');
            // 校验身份证格式
            let identifyNumberTest = true;
            let identifyType = this.prpLpayPerson.identifyType;
            let fieldvalue = this.prpLpayPerson.identifyNumber ? this.prpLpayPerson.identifyNumber.trim() : "";
            let i = fieldvalue.length;
            if (identifyType == "01") {
                var idCard15 = /\d{15}/;
                var idCard18 = /\d{17}[\d|X]/;
                if (i != 15 && i != 18) {
                    identifyNumberTest = false;
                } else {
                    if (i == 15 && !idCard15.test(fieldvalue)) {
                        identifyNumberTest = false;
                    }
                    if (i == 18) {
                        if (!idCard18.test(fieldvalue)) {
                            identifyNumberTest = false;
                        } else {
                            var code = fieldvalue.split("");
                            //∑(ai×Wi)(mod 11)
                            //加权因子
                            var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
                            //校验位
                            var parity = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];
                            var sum = 0;
                            var ai = 0;
                            var wi = 0;
                            for (var j = 0; j < 17; j++) {
                                ai = code[j];
                                wi = factor[j];
                                sum += ai * wi;
                            }
                            if (parity[sum % 11] != code[17]) {
                                identifyNumberTest = false;
                            }
                        }
                    }
                }
            }
            console.log(identifyNumberTest, 'identifyNumberTest');
            // 判断管控按钮
            if (identifyNumberTest && this.prpLpayPerson.isPrivate == "1" && this.prpLpayPerson.identifyType == '01' && year >= 46) {
                if (identifyNumberTest && this.prpLpayPerson.startDateValid && this.prpLpayPerson.startDateValid != "" && (!this.prpLpayPerson.dateValid || this.prpLpayPerson.dateValid == '')) {
                    this.longTime = true;
                    this.disabled.longTime = false;
                } else {
                    this.longTime = false;
                    this.disabled.longTime = false;
                }
            } else {
                this.longTime = false;
                this.disabled.longTime = true;
            }
        },