你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> 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];
}

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved