class IdCardHelper { static bool isNumeric(String str) { if (str.isEmpty) { return false; } return double.tryParse(str) != null; } static bool validateIDCard(String idCard) { // 校验身份证号码长度 if (idCard.length != 18) { return false; } // 校验前17位是否为数字 String idCard17 = idCard.substring(0, 17); if (!isNumeric(idCard17)) { return false; } // 校验最后一位校验码 String checkCode = getCheckCode(idCard17); if (idCard[17].toUpperCase() != checkCode) { print("checkCode: $checkCode"); return false; } return true; } static String getCheckCode(String idCard17) { List coefficients = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ]; List checkCodes = [ '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' ]; int sum = 0; for (int i = 0; i < idCard17.length; i++) { int digit = int.parse(idCard17[i]); sum += digit * coefficients[i]; } int remainder = sum % 11; return checkCodes[remainder]; } }