iOS - 正则表达式判断邮箱、身份证,车牌,URL等..是否正确:

时间:2022-04-03 03:25:01
  1. //邮箱
  2. + (BOOL) validateEmail:(NSString *)email
  3. {
  4. NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
  5. NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
  6. return [emailTest evaluateWithObject:email];
  7. }
  8. //手机号码验证
  9. + (BOOL) validateMobile:(NSString *)mobile
  10. {
  11. //手机号以13, 15,18开头,八个 \d 数字字符
  12. NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
  13. NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
  14. return [phoneTest evaluateWithObject:mobile];
  15. }
  16. //车牌号验证
  17. + (BOOL) validateCarNo:(NSString *)carNo
  18. {
  19. NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
  20. NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
  21. NSLog(@"carTest is %@",carTest);
  22. return [carTest evaluateWithObject:carNo];
  23. }
  24. //车型
  25. + (BOOL) validateCarType:(NSString *)CarType
  26. {
  27. NSString *CarTypeRegex = @"^[\u4E00-\u9FFF]+$";
  28. NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];
  29. return [carTest evaluateWithObject:CarType];
  30. }
  31. //用户名
  32. + (BOOL) validateUserName:(NSString *)name
  33. {
  34. NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
  35. NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
  36. BOOL B = [userNamePredicate evaluateWithObject:name];
  37. return B;
  38. }
  39. //密码
  40. + (BOOL) validatePassword:(NSString *)passWord
  41. {
  42. NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
  43. NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
  44. return [passWordPredicate evaluateWithObject:passWord];
  45. }
  46. //昵称
  47. + (BOOL) validateNickname:(NSString *)nickname
  48. {
  49. NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$";
  50. NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
  51. return [passWordPredicate evaluateWithObject:nickname];
  52. }
  53. //身份证号
  54. + (BOOL) validateIdentityCard: (NSString *)identityCard
  55. {
  56. BOOL flag;
  57. if (identityCard.length <= 0) {
  58. flag = NO;
  59. return flag;
  60. }
  61. NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
  62. NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
  63. return [identityCardPredicate evaluateWithObject:identityCard];
  64. }

#pragma 正则匹配手机号

+ (BOOL)checkTelNumber:(NSString *) telNumber
{
    NSString *pattern = @^1+[3578]+\d{9};
    NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern];
    BOOL isMatch = [pred evaluateWithObject:telNumber];
    return isMatch;
}
  
  
#pragma 正则匹配用户密码6-18位数字和字母组合
+ (BOOL)checkPassword:(NSString *) password
{
    NSString *pattern = @^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18};
    NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
      
}
  
#pragma 正则匹配用户姓名,20位的中文或英文
+ (BOOL)checkUserName : (NSString *) userName
{
    NSString *pattern = @^[a-zA-Z一-龥]{1,20};
    NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern];
    BOOL isMatch = [pred evaluateWithObject:userName];
    return isMatch;
      
}
  
  
#pragma 正则匹配用户身份证号15或18位
+ (BOOL)checkUserIdCard: (NSString *) idCard
{
    NSString *pattern = @(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$);
    NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern];
    BOOL isMatch = [pred evaluateWithObject:idCard];
    return isMatch;
}
  
#pragma 正则匹员工号,12位的数字
+ (BOOL)checkEmployeeNumber : (NSString *) number
{
    NSString *pattern = @^[0-9]{12};
      
    NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern];
    BOOL isMatch = [pred evaluateWithObject:number];
    return isMatch;
      
}
  
#pragma 正则匹配URL
+ (BOOL)checkURL : (NSString *) url
{
    NSString *pattern = @^[0-9A-Za-z]{1,50};
    NSPredicate *pred = [NSPredicate predicateWithFormat:@SELF MATCHES %@, pattern];
    BOOL isMatch = [pred evaluateWithObject:url];
    return isMatch;
      
}
 

/**
* 功能:验证身份证是否合法
* 参数:输入的身份证号
*/
- (BOOL)isValidPersonID {
// 判断位数
if (self.length != 15 && self.length != 18) {
return NO;
}
NSString *carid = self;
long lSumQT = 0;
// 加权因子
int R[] ={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
// 校验码
unsigned char sChecker[11]={'1','0','X', '9', '8', '7', '6', '5', '4', '3', '2'};

// 将15位身份证号转换成18位
NSMutableString *mString = [NSMutableString stringWithString:self];
if (self.length == 15) {
[mString insertString:@"19" atIndex:6];
long p = 0;
const char *pid = [mString UTF8String];

for (int i = 0; i<= 16; i++) {
p += (pid[i] - 48) * R[i];
}

int o = p % 11;
NSString *string_content = [NSString stringWithFormat:@"%c", sChecker[o]];
[mString insertString:string_content atIndex:[mString length]];
carid = mString;
}

// 判断地区码
NSString * sProvince = [carid substringToIndex:2];
if (![self areaCode:sProvince]) {
return NO;
}

// 判断年月日是否有效
// 年份
int strYear = [[self substringWithString:carid begin:6 end:4] intValue];
// 月份
int strMonth = [[self substringWithString:carid begin:10 end:2] intValue];
// 日
int strDay = [[self substringWithString:carid begin:12 end:2] intValue];

NSTimeZone *localZone = [NSTimeZone localTimeZone];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeZone:localZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date=[dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%d-%d 12:01:01",
strYear, strMonth, strDay]];
if (date == nil) {
return NO;
}

const char *PaperId = [carid UTF8String];
// 检验长度
if(18 != strlen(PaperId)) return NO;
// 校验数字
for (int i = 0; i < 18; i++) {
if ( !isdigit(PaperId[i]) && !(('X' == PaperId[i] || 'x' == PaperId[i]) && 17 == i) ) {
return NO;
}
}

// 验证最末的校验码
for (int i=0; i<=16; i++) {
lSumQT += (PaperId[i]-48) * R[i];
}

if (sChecker[lSumQT%11] != PaperId[17] ) {
return NO;
}
return YES;
}