IOS

关注公众号 jb51net

关闭
首页 > 软件编程 > IOS > iOS验证判断语句之正则表达式

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];
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

您可能感兴趣的文章:
阅读全文