// 匹配中国大陆手机号
bool isPhone(String input) {
return RegExp(r"^1[3456789]\d{9}$").hasMatch(input);
}
// 匹配邮箱
bool isEmail(String input) {
return RegExp(r"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$").hasMatch(input);
}
// 匹配18位身份证号,要严格校验,要根据最后一位校验
bool isValidateIDCard(String idCard) {
// 正则表达式匹配身份证号,前 17 位为数字,最后一位可以为数字或大写字母X
RegExp regExp = RegExp(r'^\d{17}[\dX]$');
if (!regExp.hasMatch(idCard)) {
return false;
}
// 计算校验码
List<int> idCardDigits = idCard.substring(0, 17).split('').map(int.parse).toList();
Map<int, int> idCardWi = {
0: 7, 1: 9, 2: 10, 3: 5, 4: 8, 5: 4, 6: 2, 7: 1, 8: 6, 9: 3, 10: 7, 11: 9, 12: 10, 13: 5, 14: 8, 15: 4, 16: 2
};
int sum = 0;
for (int i = 0; i < idCardDigits.length; i++) {
sum += idCardDigits[i] * idCardWi[i]!;
}
int remainder = sum % 11;
Map<int, String> idCardValidator = {
0: '1', 1: '0', 2: 'X', 3: '9', 4: '8', 5: '7', 6: '6', 7: '5', 8: '4', 9: '3', 10: '2'
};
String lastChar = idCard.substring(17, 18);
return lastChar == idCardValidator[remainder];
}
// 匹配密码至少包含 数字和英文,支持特殊字符(.@#%&-_),长度6-20
bool isPassword(String input) {
return RegExp(r"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z.@#%&-_]{6,20}$").hasMatch(input);
}
// 匹配验证码,6位英文大小写字母或数字
bool isValidateCode(String input) {
return RegExp(r"^[0-9A-Za-z]{6}$").hasMatch(input);
}
// 匹配中国大陆公司名称,仅能输入中文简体,且长度为6个字以上,最后两个字必须为公司
bool isCompanyName(String input) {
return RegExp(r"^[\u4e00-\u9fa5]{4,}公司$").hasMatch(input);
}
08-02
869
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountBlack.png)
06-09
1927
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountBlack.png)