isIDCard18Exact static method

bool isIDCard18Exact(
  1. String input
)

Return whether input matches regex of exact id card number which length is 18. 返回输入是否匹配长度为18的id卡号的正则表达式。

Implementation

static bool isIDCard18Exact(String input) {
  if (isIDCard18(input)) {
    final List<int> factor = [
      7,
      9,
      10,
      5,
      8,
      4,
      2,
      1,
      6,
      3,
      7,
      9,
      10,
      5,
      8,
      4,
      2
    ];
    final List<String> suffix = [
      '1',
      '0',
      'X',
      '9',
      '8',
      '7',
      '6',
      '5',
      '4',
      '3',
      '2'
    ];
    if (cityMap.isEmpty) {
      final List<String> list = ID_CARD_PROVINCE_DICT;
      final List<MapEntry<String, String>> mapEntryList = [];
      for (int i = 0, length = list.length; i < length; i++) {
        final List<String> tokens = list[i].trim().split('=');
        final MapEntry<String, String> mapEntry =
            MapEntry(tokens[0], tokens[1]);
        mapEntryList.add(mapEntry);
      }
      cityMap.addEntries(mapEntryList);
    }
    if (cityMap[input.substring(0, 2)] != null) {
      int weightSum = 0;
      for (int i = 0; i < 17; ++i) {
        weightSum += (input.codeUnitAt(i) - '0'.codeUnitAt(0)) * factor[i];
      }
      final int idCardMod = weightSum % 11;
      // [REGEX_ID_CARD18] allows a case-insensitive trailing `X`; normalize
      // it before comparing against the uppercase suffix table.
      final String idCardLast =
          String.fromCharCode(input.codeUnitAt(17)).toUpperCase();
      return idCardLast == suffix[idCardMod];
    }
  }
  return false;
}