iOS regular expression to judge mobile phone number and fixed line

  • 2021-12-13 09:51:22
  • OfStack

Without saying much, please look at the code:


{
  if (mobileNum.length != 11)
  {
    return NO;
  }
  /**
   *  Mobile phone number : 
   * 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]
   *  Mobile number segment : 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
   *  Unicom number segment : 130,131,132,155,156,185,186,145,176,1709
   *  Electrical signal segment : 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: 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: 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: China Telecom
   * 133,153,180,181,189,177,1700
   */
  NSString *CT = @"(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";
  /**
   25     *  Fixed telephone and PHS in mainland China 
   26     *  Area code: 010,020,021,022,023,024,025,027,028,029
   27     *  Number: 7 Bit or 8 Bit 
   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))
  {
    return YES;
  }
  else
  {
    return NO;
  }
}

Expand:

If you simply match whether it is a mobile phone number, you don't need as many lines of code as above, you can simply write it like this:


NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[0678])\\d{8}$";
NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
return [regextestmobile evaluateWithObject:mobileNum];

If you need to match whether it is a mobile/Unicom/Telecom mobile phone number.

This is how to judge the mobile phone number:


+ (BOOL)isChinaMobile:(NSString *)phoneNum
{
  NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";
  NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
  return [regextestcm evaluateWithObject:phoneNum];
}

To judge Unicom's mobile phone number in the same way, just change our regular string into the string to judge Unicom's mobile phone number above. Just change the regular expression when judging which one. In addition, on this basis, we can also combine to judge which operator's mobile phone number is, and the code is as follows:


+ (NSString *)getPhoneNumType:(NSString *)phoneNum
{
  return [self isChinaMobile:phoneNum]? @" China Mobile ": ([self isChinaUnicom:phoneNum]? @" China Unicom ":([self isChinaTelecom:phoneNum]? @" China Telecom ": @" Unknown "));
}

Related articles: