当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS验证手机号的正则表达式

iOS验证手机号的正则表达式

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例为大家分享了ios正则验证手机号的代码片段,供大家参考,具体内容如下

//判断手机号码格式是否正确
+ (bool)valimobile:(nsstring *)mobile
{
  mobile = [mobile stringbyreplacingoccurrencesofstring:@" "withstring:@""];
  if (mobile.length != 11)
  {
    return no;
  }else{
    /**
     * 移动号段正则表达式
     */
    nsstring *cm_num = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
    /**
     * 联通号段正则表达式
     */
    nsstring *cu_num = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
    /**
     * 电信号段正则表达式
     */
    nsstring *ct_num = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
    nspredicate *pred1 = [nspredicatepredicatewithformat:@"self matches %@", cm_num];
    bool ismatch1 = [pred1 evaluatewithobject:mobile];
    nspredicate *pred2 = [nspredicatepredicatewithformat:@"self matches %@", cu_num];
    bool ismatch2 = [pred2 evaluatewithobject:mobile];
    nspredicate *pred3 = [nspredicatepredicatewithformat:@"self matches %@", ct_num];
    bool ismatch3 = [pred3 evaluatewithobject:mobile];
 
    if (ismatch1 || ismatch2 || ismatch3) {
      return yes;
    }else{
      return no;
    }
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网