Certificate verification

Time:2021-2-27

Verification of ID card

function checkIDCard(idcode) {
  //Regular thinking
  /*
    The first one can't be zero
    The second to sixth can be 0-9
    The seventh to tenth places are the year, so the seventh and eighth places are 19 or 20
    The 11th and 12th digits are the month, and these two digits are between 01 and 12
    The thirteenth and fourteenth digits are dates, values from 01 to 31
    15、 Sixteen and seventeen are numbers 0-9
    The eighteen digits could be numbers 0-9, or X
    */
  var idcard_patter =
        /^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0|1][0-9])([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X]|[x])$/

  //Judge whether the format is correct
  var format = idcard_patter.test(idcode)

  return !!format
}

Mobile phone number verification

function checkPhone(phone) {
  if (!(/^1[3456789]\d{9}$/.test(phone))) {
    return false
  } else {
    return true
  }
}

Verify the officer certificate

function isOfficerCard(card) {
  //Rules: Jun / Bing / Shi / Wen / Zhi / Guang / (other Chinese) + "Zi Di" + 4 to 8 letters or numbers + "Hao"
  //Sample: Jun Zi no.2001988, Shi Zi no.p011816x
  Var reg = / ^ [[u4e00 - [u9fa5] (word number) ([0-9a-zA-Z] {4,8}) (sign?)$/
  if (reg.test(card) === false) {
    return {
      'status': 0,
      'msg ':'officer's license number is not compliant'
    }
  } else {
    return {
      'status': 1,
      'msg ':'verification passed'
    }
  }
}

passport

function isPassPortCard(card) {
  //Rules: 14 / 15 + 7 digits, G + 8 digits, P + 7 digits, S / D + 7 or 8 digits, etc
  //Samples: 141234567, g12345678, p1234567
  var reg = /^([a-zA-z]|[0-9]){5,17}$/
  if (reg.test(card) === false) {
    return {
      'status': 0,
      'msg ':'Passport number not in conformity'
    }
  } else {
    return {
      'status': 1,
      'msg ':'verification passed'
    }
  }
}

Pass for Hong Kong and Macao residents to and from the mainland

function isHKCard(card) {
  //Rule: H / M + 10 or 6 digits
  //Sample: h1234567890
  var reg = /^([A-Z]\d{6,10}(\(\w{1}\))?)$/
  if (reg.test(card) === false) {
    return {
      'status': 0,
      'msg ':'The number of Hong Kong and Macao residents' pass to and from the mainland is not up to standard'
    }
  } else {
    return {
      'status': 1,
      'msg ':'verification passed'
    }
  }
}

Taiwan ID card

function checkTWIdCard(idNo) {
  if (idNo == null || idNo == '') {
    return {
      'respCode': '9',
      'respdesc ':'null'
    }
  }
  if (idNo.length != 10) {
    return {
      'respCode': '1',
      'respdesc ':'length of national identity card unified number is incorrect (10) "
    }
  }
  var eng = idNo.substring(0, 1)
  var num = idNo.substring(1, 10)
  var sex = idNo.substring(1, 2)
  eng = eng.toUpperCase()
  Var Regnum = / ^ [0-9] +.? [0-9] * // / judge whether the string is a number // judge a positive integer / [1 − 9] + [0 − 9] *] * /; // judge whether the string is a number // judge a positive integer / [1 − 9] + [0 − 9] *] * */
  var regEng = /^[A-Za-z]+$/
  if (!regNum.test(num)) {
    return {
      'respCode': '1',
      'respdesc ':'National identity card unified number is incorrect'
    }
  }
  if (sex != 1 && sex != 2) {
    return {
      'respCode': '1',
      'respdesc ':'National identity card unified number is incorrect'
    }
  }
  if (!regEng.test(eng)) {
    return {
      'respCode': '1',
      'respdesc ':'National identity card unified number is incorrect'
    }
  }
  // var key=[‘A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’,’J’,’K’,’L’,’M’,’N’,’P’,’Q’,’R’,’S’,’T’,’U’,’V’,’X’,’Y’,’W’,’Z’,’I’,’O’];
  var key = 'ABCDEFGHJKLMNPQRSTUVXYWZIO'
  var value = ['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'
  ]
  var pos = key.indexOf(eng)
  if (pos == -1) {
    return {
      'respCode': '1',
      'respdesc ':'National identity card unified number is incorrect'
    }
  }
  var x1 = value[pos].substring(0, 1)
  var x2 = value[pos].substring(1, 2)
  var y = parseInt(x1) + 9 * parseInt(x2) + 8 * parseInt(num.substring(0, 1)) + 7 * parseInt(num.substring(1, 2)) + 6 *
        parseInt(num.substring(2, 3)) + 5 * parseInt(num.substring(3, 4)) + 4 * parseInt(num.substring(4, 5)) + 3 * parseInt(
    num.substring(5, 6)) + 2 * parseInt(num.substring(6, 7)) + parseInt(num.substring(7, 8))
  var checkResult = y % 10
  if ((10 - checkResult) == num.substring(8, 9)) {
    return {
      'respCode': '0',
      'respDesc': 'success'
    }
  } else {
    if (checkResult == 10 && num.substring(8, 9) == 0) {
      //If the result after remainder is 0, then the number at the end is 0
      return {
        'respCode': '0',
        'respDesc': 'success'
      }
    } else {
      return {
        'respCode': '1',
        'respdesc ':'National identity card unified number is incorrect'
      }
    }
  }
}

travel passes for Taiwan residents to enter or leave the mainland

function isTWCard(card) {
  //Rules: new 8-digit or 18 digit, old 10 digit plus English letters
  //Sample: 12345678 or 1234567890b
  var reg = /^\d{8}|^[a-zA-Z0-9]{10}|^\d{18}$/
  if (reg.test(card) === false) {
    return {
      'status': 0,
      'msg ':'The number of Taiwan residents' pass to and from the mainland is not up to standard'
    }
  } else {
    return {
      'status': 1,
      'msg ':'verification passed'
    }
  }
}