iOS开发验证判断语句之正则表达式小结

来源:爱站网时间:2020-03-20编辑:网友分享
我们都知道,开发过程中一直离不开常用的验证格式,而我们通常会创建工具类来具体管理这些验证方法,从而方便开发过程,下面我们跟着爱站技术频道一起来了解iOS开发验证判断语句之正则表达式小结吧!

我们都知道,开发过程中一直离不开常用的验证格式,而我们通常会创建工具类来具体管理这些验证方法,从而方便开发过程,下面我们跟着爱站技术频道一起来了解iOS开发验证判断语句之正则表达式小结吧!

一、验证移动手机号:

+ (BOOL)isMobile:(NSString*)mobile
{
  NSString *regex = @"^1+[34578]+\d{9}";
  NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  return [pred evaluateWithObject:mobile];
}

二、邮箱验证:

+ (BOOL)isEmail:(NSString*)email
{
  NSString *regex = @"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";
  NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  return [pred evaluateWithObject:email];
}

三、整形验证

+ (BOOL)isPureInt:(NSString*)string{
  NSScanner* scan = [NSScanner scannerWithString:string];
  int val;
  return[scan scanInt:&val] && [scan isAtEnd];
}

四、浮点型验证

- (BOOL)isPureFloat:(NSString*)string{
  NSScanner* scan = [NSScanner scannerWithString:string];
  float val;
  return[scan scanFloat:&val] && [scan isAtEnd];
}

五、中文验证

+ (BOOL)isChinese:(NSString*)chinese
{
  NSString *regex = @"^[\u4e00-\u9fa5]";
  NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  return [pred evaluateWithObject: chinese];
}

六、网址URL验证

+ (BOOL)isURL:(NSString*)url
{
  NSString *regex = @"[a-zA-z]+://[^\s]*";
  NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  return [pred evaluateWithObject: url];
}

七、身份证号验证(比较繁琐)

+ (BOOL)verifyIDCardNumber:(NSString *)IDCardNumber 
{ 
IDCardNumber = [IDCardNumber stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
if ([IDCardNumber length] != 18) 
{ 
return NO; 
} 
NSString *mmdd = @”(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8])))”; 
NSString *leapMmdd = @”0229”; 
NSString *year = @”(19|20)[0-9]{2}”; 
NSString *leapYear = @”(19|20)(0[48]|[2468][048]|[13579][26])”; 
NSString *yearMmdd = [NSString stringWithFormat:@”%@%@”, year, mmdd]; 
NSString *leapyearMmdd = [NSString stringWithFormat:@”%@%@”, leapYear, leapMmdd]; 
NSString *yyyyMmdd = [NSString stringWithFormat:@”((%@)|(%@)|(%@))”, yearMmdd, leapyearMmdd, @”20000229”]; 
NSString *area = @”(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|82|[7-9]1)[0-9]{4}”; 
NSString *regex = [NSString stringWithFormat:@”%@%@%@”, area, yyyyMmdd , @”[0-9]{3}[0-9Xx]”];

NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if (![regexTest evaluateWithObject:IDCardNumber])
{
  return NO;
}
int summary = ([IDCardNumber substringWithRange:NSMakeRange(0,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(10,1)].intValue) *7
+ ([IDCardNumber substringWithRange:NSMakeRange(1,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(11,1)].intValue) *9
+ ([IDCardNumber substringWithRange:NSMakeRange(2,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(12,1)].intValue) *10
+ ([IDCardNumber substringWithRange:NSMakeRange(3,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(13,1)].intValue) *5
+ ([IDCardNumber substringWithRange:NSMakeRange(4,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(14,1)].intValue) *8
+ ([IDCardNumber substringWithRange:NSMakeRange(5,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(15,1)].intValue) *4
+ ([IDCardNumber substringWithRange:NSMakeRange(6,1)].intValue + [IDCardNumber substringWithRange:NSMakeRange(16,1)].intValue) *2
+ [IDCardNumber substringWithRange:NSMakeRange(7,1)].intValue *1 + [IDCardNumber substringWithRange:NSMakeRange(8,1)].intValue *6
+ [IDCardNumber substringWithRange:NSMakeRange(9,1)].intValue *3;
NSInteger remainder = summary % 11;
NSString *checkBit = @"";
NSString *checkString = @"10X98765432";
checkBit = [checkString substringWithRange:NSMakeRange(remainder,1)];// 判断校验位
return [checkBit isEqualToString:[[IDCardNumber substringWithRange:NSMakeRange(17,1)] uppercaseString]];
}

八、一般用户名的验证

+ (BOOL) isUserName:(NSString *)name
{
  NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
  NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];  
  return [userNamePredicate evaluateWithObject:name];
}

上面介绍的知识是iOS开发验证判断语句之正则表达式小结,如果你也是对这些知识感兴趣的话,不妨可以尝试一下爱站技术频道小编的介绍吧!

上一篇:iOS开发之银行卡号识别

下一篇:iOS中的各种颜色设置总结大全

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载