由于新时代,各种网络运营商出现,比如阿里的蚂蚁宝卡,腾讯的大王卡等等,所以常规的手机号正则表达式已经满足不了现实需求,下面提供一个新的正则表达式(这里同时记录下一些比较常用的正则)
代码示例
/**
验证码手机号
@param mobileNum 手机号
@return YES 通过 NO 不通过
*/
+ (BOOL)validateContactNumber:(NSString *)mobileNum
{
/**
* 手机号码:
* 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 16[6], 17[5, 6, 7, 8], 18[0-9], 170[0-9], 19[89]
* 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705,198
* 联通号段: 130,131,132,155,156,185,186,145,175,176,1709,166
* 电信号段: 133,153,180,181,189,177,1700,199
*/
NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|6[6]|7[05-8]|8[0-9]|9[89])\\d{8}$";
/**
* 中国移动:China Mobile
* 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705,198
*/
NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478]|9[8])\\d{8}$)|(^1705\\d{7}$)";
/**
* 中国联通:China Unicom
* 130,131,132,155,156,185,186,145,175,176,1709,166
*/
NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|66|7[56]|8[56])\\d{8}$)|(^1709\\d{7}$)";
/**
* 中国电信:China Telecom
* 133,153,180,181,189,177,1700,199
*/
NSString *CT = @"(^1(33|53|77|8[019]|99)\\d{8}$)|(^1700\\d{7}$)";
/**
* 大陆地区固话及小灵通
* 区号:010,020,021,022,023,024,025,027,028,029
* 号码:七位或八位
*/
NSString * PHS = @"^0(10|2[0-5789]|\\d{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];
NSPredicate *regextestPHS = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS];
if(([regextestmobile evaluateWithObject:mobileNum] == YES)
|| ([regextestcm evaluateWithObject:mobileNum] == YES)
|| ([regextestct evaluateWithObject:mobileNum] == YES)
|| ([regextestcu evaluateWithObject:mobileNum] == YES)
|| ([regextestPHS evaluateWithObject:mobileNum] == YES)) {
return YES;
} else {
return NO;
}
}
网友评论