iOS开发收集——验证判断语句·正则表达式

SanDunbabin 3年前
   <p>最近,在公司接手重构一个项目,发现之前的开发在验证格式这块写的太乱了,到处都有相关的验证代码,故而,写了这篇文章,供自己收藏,也分享给大家。</p>    <p>开发一直离不开一些常用验证格式,比如:邮箱,手机号等等,在开发过程中一般都会新建一个工具类,专门管理这些验证方式,简便开发过程。一般都采用正则表达式的形式来做判断,本文列举一些项目中非常实用的一些正则表达式的判断语句,以后开发起来直接复制粘贴就可以,这样大大节省了开发时间,不多说,直接上代码。</p>    <p><strong>1.验证移动手机号:</strong></p>    <pre>  <code class="language-objectivec">+ (BOOL)isMobile:(NSString*)mobile  {      NSString *regex = @"^1+[34578]+\d{9}";      NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];      return [pred evaluateWithObject:mobile];  }</code></pre>    <p><strong>2.邮箱验证:</strong></p>    <pre>  <code class="language-objectivec">+ (BOOL)isEmail:(NSString*)email  {      NSString *regex = @"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$";      NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];      return [pred evaluateWithObject:email];  }</code></pre>    <p><strong>3.整形验证</strong></p>    <pre>  <code class="language-objectivec">+ (BOOL)isPureInt:(NSString*)string{      NSScanner* scan = [NSScanner scannerWithString:string];      int val;      return[scan scanInt:&val] && [scan isAtEnd];  }</code></pre>    <p><strong>4.浮点型验证</strong></p>    <pre>  <code class="language-objectivec">- (BOOL)isPureFloat:(NSString*)string{      NSScanner* scan = [NSScanner scannerWithString:string];      float val;      return[scan scanFloat:&val] && [scan isAtEnd];  }</code></pre>    <p><strong>5.中文验证</strong></p>    <pre>  <code class="language-objectivec">+ (BOOL)isChinese:(NSString*)chinese  {      NSString *regex = @"^[\u4e00-\u9fa5]";      NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];      return [pred evaluateWithObject: chinese];  }</code></pre>    <p><strong>6.网址URL验证</strong></p>    <pre>  <code class="language-objectivec">+ (BOOL)isURL:(NSString*)url  {      NSString *regex = @"[a-zA-z]+://[^\s]*";      NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];      return [pred evaluateWithObject: url];  }</code></pre>    <p><strong>7.身份证号验证(比较繁琐)</strong></p>    <pre>  <code class="language-objectivec">+ (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]];  }</code></pre>    <p><strong>8.一般用户名的验证</strong></p>    <pre>  <code class="language-objectivec">+ (BOOL) isUserName:(NSString *)name  {      NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";      NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];          return  [userNamePredicate evaluateWithObject:name];  }</code></pre>    <p> </p>    <p>来自:http://www.jianshu.com/p/35865204d037</p>    <p> </p>