input框录入身份证自动填写性别年龄

1.data

      //基础规范
      rules: {
        idcard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" },
          { validator: this.validID, trigger: ["blur", "change"] }
        ]
      },

2.1 methods

    /* 身份证验证 */
    async validID(rule, value, callback) {
      // 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
      let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
      if (reg.test(value)) {
        let info = await this.go(value.length);
        console.log(info);
        await this.setInfo(info);
      } else {
        this.gender = "";
        this.birthday = "";
        callback(new Error("身份证号码不正确"));
      }
    },

2.2 methods

    /* 设置属性 */
    setInfo(info) {
      var that = this;
      that.birthday = info[1];
      that.gender = info[0];
    },

2.3 methods

    // 实现自动生成生日,性别,年龄
    go(val) {
      let iden = this.person.idcard;
      let sex = null;
      let birth = null;
      let myDate = new Date();
      let month = myDate.getMonth() + 1;
      let day = myDate.getDate();
      let age = 0;
      if (val === 18) {
        sex = iden.substring(16, 17);
        birth =
          iden.substring(6, 10) +
          "-" +
          iden.substring(10, 12) +
          "-" +
          iden.substring(12, 14);
        if (
          iden.substring(10, 12) < month ||
          (iden.substring(10, 12) == month && iden.substring(12, 14) <= day)
        )
          age++;
      }
      if (val === 15) {
        sex = iden.substring(13, 14);
        birth =
          "19" +
          iden.substring(6, 8) +
          "-" +
          iden.substring(8, 10) +
          "-" +
          iden.substring(10, 12);
        if (
          iden.substring(8, 10) < month ||
          (iden.substring(8, 10) == month && iden.substring(10, 12) <= day)
        )
          age++;
      }

      if (sex % 2 === 0) sex = "0";
      else sex = "1";
      let info = [];
      info.push(sex, birth);
      return info;
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值