vue 身份证验证

 数据格式

data () {
    return {
      show: false,
      type: 'add',
      saveLoading: false,
      dataobj: {
        name: '', // 姓名
        idType: '', // 证件类型
        id: '', // 证件号
        birthday: '', // 出生日期
        gender: '', // 性别
        relations: '', // 关系
        insuredAmount: '', // 保额
        paymentMethod: '', // 交费方式期限
        premium: '' // 保费
      }
    }
  },

 

validateIdCard (id, backInfo) {
      let info = {
        y: '1900',
        m: '01',
        d: '01',
        sex: 'male',
        valid: false,
        length: 0
      }
      if (typeof id !== 'string') return this.back(info, backInfo)
      // 18
      if (/^\d{17}[0-9x]$/i.test(id)) {
        if (!this.initDate(18, info, id)) return this.back(info, backInfo)
        id = id.toLowerCase().split('')
        let wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
        let y = '10x98765432'.split('')
        let sum = 0
        for (let i = 0; i < 17; i++) sum += wi[i] * id[i]
        if (y[sum % 11] === id.pop().toLowerCase()) info.valid = true
        return this.back(info, backInfo)
      } else if (/^\d{15}$/.test(id)) { // 15
        if (this.initDate(15, info, id)) info.valid = true
        return this.back(info, backInfo)
      } else {
        return this.back(info, backInfo)
      }
    },
    back (info, backInfo) {
      return backInfo ? info : info.valid
    },
    initDate (length, info, id) {
      info.length = length
      let a
      a = (length === 15) ? 0 : 2 // 15:18
      let temp
      info.y = (a ? '' : '19') + id.substring(6, 8 + a)
      info.m = id.substring(8 + a, 10 + a)
      info.d = id.substring(10 + a, 12 + a)
      info.sex = id.substring(14, 15 + a) % 2 === 0 ? 'female' : 'male'
      temp = new Date(info.y, info.m - 1, info.d)
      return (temp.getFullYear() === info.y * 1) &&
            (temp.getMonth() + 1 === info.m * 1) &&
            (temp.getDate() === info.d * 1)
    },

身份证失去光标事件判断身份证号是否正确

IdCardBlur () {
      if (this.validateIdCard(this.dataobj.id)) {
        if (this.dataobj.id.length === 18) {
          let borthNumY = this.dataobj.id.substr(6, 4)
          let borthNumM = this.dataobj.id.substr(10, 2)
          let borthNumD = this.dataobj.id.substr(12, 2)
          let SexNum = this.dataobj.id.substr(16, 1)
          this.dataobj.birthday = borthNumY + '-' + borthNumM + '-' + borthNumD
          if (SexNum % 2 === 0) {
            this.dataobj.gender = '1'
          } else {
            this.dataobj.gender = '0'
          }
        } else if (this.dataobj.id.length === 15) {
          let borthNumY = this.dataobj.id.substr(6, 2)
          let borthNumM = this.dataobj.id.substr(8, 2)
          let borthNumD = this.dataobj.id.substr(10, 2)
          let SexNum = this.dataobj.id.substr(14, 1)
          this.dataobj.birthday = '19' + borthNumY + '-' + borthNumM + '-' + borthNumD
          if (SexNum % 2 === 0) {
            this.dataobj.gender = '1'
          } else {
            this.dataobj.gender = '0'
          }
        }
      } else {
        console.log('请输入正确的身份证号')
      }
    },

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值