欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-21 20:52:10
本文实例为大家分享了ios正则验证手机号的代码片段,供大家参考,具体内容如下 //判断手机号码格式是否正确 + (bool)valimobile:(nsstri...

本文实例为大家分享了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;
    }
  }
}

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

上一篇:

下一篇: