1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- 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<int> coefficients = [
- 7,
- 9,
- 10,
- 5,
- 8,
- 4,
- 2,
- 1,
- 6,
- 3,
- 7,
- 9,
- 10,
- 5,
- 8,
- 4,
- 2
- ];
- List<String> 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];
- }
- }
|