isChineseIdNumber property
bool
get
isChineseIdNumber
Returns true if id is a valid Chinese resident ID number.
Implementation
bool get isChineseIdNumber {
if (this == null) {
return false;
}
// 1. Basic format
if (this!.length != 18) return false;
final digits = this!.split('');
if (digits.sublist(0, 17).any((c) => !RegExp(r'[0-9]').hasMatch(c))) {
return false;
}
// 2. Birthday (positions 7-14: YYYYMMDD)
final year = int.parse(this!.substring(6, 10));
final month = int.parse(this!.substring(10, 12));
final day = int.parse(this!.substring(12, 14));
if (month < 1 || month > 12) return false;
if (day < 1 || day > 31) return false;
// Validate real date (including leap year)
final DateTime? birthDate;
try {
birthDate = DateTime(year, month, day);
} on ArgumentError {
return false;
}
if (birthDate.year != year ||
birthDate.month != month ||
birthDate.day != day) {
return false;
}
// 3. Check digit (ISO 7064, MOD 11-2)
const weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
const checkMap = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
var sum = 0;
for (var i = 0; i < 17; i++) {
sum += int.parse(digits[i]) * weights[i];
}
final expectedCheck = checkMap[sum % 11];
final actualCheck = digits[17].toUpperCase();
return expectedCheck == actualCheck;
}