iOS开发验证判断语句之正则表达式小结(号码邮箱身份证)

其他验证可查看URL:判断01  判断02  判断03  判断04

一、验证移动手机号:

+ (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];

  intval;

  return[scan scanInt:&val] && [scan isAtEnd];

}


四、浮点型验证

- (BOOL)isPureFloat:(NSString*)string{

  NSScanner* scan = [NSScanner scannerWithString:string];

  floatval;

  return[scan scanFloat:&val] && [scan isAtEnd];

}


五、中文验证

+ (BOOL)isURL:(NSString*)url{

  NSString *regex = @"[a-zA-z]+://[^\s]*";

  NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

  return[pred evaluateWithObject: url];

}


六、网址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){

returnNO;

}

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]){

  returnNO;

}

intsummary = ([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];

}


九·判断手机号码、固话

{

  if(mobileNum.length != 11) {

    returnNO;

  }

  /**

   * 手机号码:

   * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9]

   * 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705

   * 联通号段: 130,131,132,155,156,185,186,145,176,1709

   * 电信号段: 133,153,180,181,189,177,1700

   */

  NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[0678])\\d{8}$";

  /**

   * 中国移动:China Mobile

   * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705

   */

  NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";

  /**

   * 中国联通:China Unicom

   * 130,131,132,155,156,185,186,145,176,1709

   */

  NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)";

  /**

   * 中国电信:China Telecom

   * 133,153,180,181,189,177,1700

   */

  NSString *CT = @"(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";

  /**

   25     * 大陆地区固话及小灵通

   26     * 区号:010,020,021,022,023,024,025,027,028,029

   27     * 号码:七位或八位

   28     */

  //  NSString * PHS = @"^(0[0-9]{2})\\d{8}$|^(0[0-9]{3}(\\d{7,8}))$";

  NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];

  NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];

  NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];

  NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];

  if(([regextestmobile evaluateWithObject:mobileNum] == YES)

    || ([regextestcm evaluateWithObject:mobileNum] == YES)

    || ([regextestct evaluateWithObject:mobileNum] == YES)

    || ([regextestcu evaluateWithObject:mobileNum] == YES))  {

    returnYES;

  }  else{

    returnNO;  }}


你可能感兴趣的:(iOS开发验证判断语句之正则表达式小结(号码邮箱身份证))