isIDCard18Exact static method

bool isIDCard18Exact(
  1. String input
)

Return whether input matches regex of exact id card number which length is 18.

Implementation

static bool isIDCard18Exact(String input) {
  if (isIDCard18(input)) {
    List<int> factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    List<String> suffix = [
      '1',
      '0',
      'X',
      '9',
      '8',
      '7',
      '6',
      '5',
      '4',
      '3',
      '2'
    ];
    if (cityMap.isEmpty) {
      List<String> list = ID_CARD_PROVINCE_DICT;
      List<MapEntry<String, String>> mapEntryList = [];
      for (int i = 0, length = list.length; i < length; i++) {
        List<String> tokens = list[i].trim().split('=');
        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];
      }
      int idCardMod = weightSum % 11;
      String idCardLast = String.fromCharCode(input.codeUnitAt(17));
      return idCardLast == suffix[idCardMod];
    }
  }
  return false;
}