isIdCard function
Implementation
bool isIdCard(String idCard) {
if (idCard.isEmpty) {
print('身份证不能为空');
return false;
} else if (!(idCard.length == 15 || idCard.length == 18)) {
print('身份证必须是15位或者18位');
return false;
}
if (idCard.length == 15 && ((idCard = wrap15To18(idCard)).isEmpty)) {
return false;
}
String ai = "";
String aiCache = ""; //大写X
if (idCard.length == 18) {
ai = idCard.substring(0, 17);
} else if (idCard.length == 15) {
ai = idCard.substring(0, 6) + "19" + idCard.substring(6, 15);
}
if (!_isDigital(ai)) {
print('身份证处最后一位必须是纯数字');
return false;
}
String strYear = ai.substring(6, 10); // 年份
String strMonth = ai.substring(10, 12); // 月份
String strDay = ai.substring(12, 14); // 月份
if (_dateRegExp.hasMatch(strYear + "-" + strMonth + "-" + strDay)) {
print('出生年月日无效');
return false;
}
int yearDelta = int.parse(strYear) - _currentYear;
//身份证上的年份与当前系统年份差值太大
//根据需要自定义修改该值
if (yearDelta.abs() > _YEAR_DELTA) {
print('年份不对,距离当前年份差量过大');
return false;
}
if (int.parse(strMonth) > 12 || int.parse(strMonth) <= 0) {
print('月份不对');
return false;
}
if (int.parse(strDay) > 31 || int.parse(strDay) <= 0) {
print('日期不对');
return false;
}
if (!_getAreaCode().containsKey(ai.substring(0, 2))) {
print('地区编码错误');
return false;
}
int totalMulAiWi = 0;
for (int i = 0; i < 17; i++) {
totalMulAiWi = totalMulAiWi + int.parse(ai.substring(i, i + 1)) * int.parse(_Wi[i]);
}
int modValue = totalMulAiWi % 11;
String strVerifyCode = _ValCodeArr[modValue];
//兼容大写字母X
if ("x" == strVerifyCode) {
aiCache = ai + "X";
}
ai = ai + strVerifyCode;
if (idCard.length == 18) {
if (!(ai == idCard) && !(aiCache == idCard)) {
return false;
}
} else {
return true;
}
return true;
}