app-starlock/lib/tools/regularExpression.dart

42 lines
1.2 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class RegularExpression {
bool isPhoneNumber(String input) {
// 手机号正则表达式这里简化为11位数字
final RegExp phoneRegExp = RegExp(r'^\d{11}$');
return phoneRegExp.hasMatch(input);
}
bool isEmail(String input) {
// 邮箱正则表达式,这里简化为常见格式
final RegExp emailRegExp =
RegExp(r'^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$');
return emailRegExp.hasMatch(input);
}
static RegExp urlRegExp = RegExp(
r'https?:\/\/\S+',
caseSensitive: false,
);
// 验证登录密码是否至少包含数字、字母、符号中的两种
bool validateString(String value) {
// 正则表达式
RegExp regExpNum = RegExp(r'\d'); // 数字
RegExp regExpLetter = RegExp(r'[a-zA-Z]'); // 字母
RegExp regExpSymbol = RegExp(r'[!@#\$&*~]'); // 符号
// 将字符串与每个正则表达式进行比较
bool hasNum = regExpNum.hasMatch(value);
bool hasLetter = regExpLetter.hasMatch(value);
bool hasSymbol = regExpSymbol.hasMatch(value);
// 计算匹配的数量
int count = 0;
if (hasNum) count++;
if (hasLetter) count++;
if (hasSymbol) count++;
// 如果数量大于或等于2返回true
return count >= 2;
}
}