用户信息检验

身份证校验

  • 使用18位校验码进行加权校验身份信息
  • 前6位计算省市区、后4位计算性别
class ValiId {
  constructor(code = '') {
    this.id = code
    this.status = true
    this.errorMap = new Map([
      ['01', '身份证输入错误']
    ])
  }
  init() {
    this.vailIdCard()
    this.domicile()
    return this
  }
  error(code) {
    this.status = false
    return this.errorMap.get(code)
  }
  vailIdCard() {
    let id = this.id
    if (typeof (id) !== 'string'
      || id.length != 18
      || !/[0-9xX]/.test(id[17])) {
      this.error('01')
      return this
    }
    let result = 0
    let ans = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    for (let i = 0; i < 17; i++) {
      if (!/[0-9]/.test(id[i])) {
        this.error('01')
        return this
      }
      result += Number.parseInt(id[i]) * ans[i]
    }
    if (/[xX]/.test(id[17])) {
      result += 10
    } else {
      result += Number.parseInt(id[17])
    }
    return result % 11 === 1
  }
  domicile() {
    let province = 2
    let city = 3
    let area = 4
    this.domicile = {
      domicile: province + city + area,
      province, city, area
    }
  }
}

const test = new VailId('220422199207056311').init()
console.log(test)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
上次更新:
作者: Aziz-pang