您现在的位置是:首页 > 文章详情

iOS开发-UITextField手机号和邮箱验证

日期:2017-06-27点击:467

不管是网页是手机,用户注册登录的时候绝大数时候都需要手机号码和邮箱地址,而且有些App会限制只能使用手机号注册,iOS方面邮箱正则比较简单,不过手机号码验证找了一下网上的,发现三大运营商的号码段有所变化,通过最新的号码段判断用户手机验证的时候出错概率会小,如果有遗漏的号码段,欢迎补充。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*手机验证 */
+ ( BOOL )isMobileNumber:( NSString  *)mobileNum {
     /**
      * 手机号码
      * 移动:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
      * 联通:130/131/132/155/156/185/186/145/176
      * 电信:133/153/180/181/189/177
      */
     NSString  *MOBILE = @ "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$" ;
     /**
      * 中国移动:China Mobile
      * 134[0-8]/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
      */
     NSString  *CM = @ "^1(34[0-8]|(3[5-9]|5[0127-9]|8[23478]|47|78)\\d)\\d{7}$" ;
     /**
      * 中国联通:China Unicom
      * 130/131/132/152/155/156/185/186/145/176
      */
     NSString  *CU = @ "^1(3[0-2]|5[256]|8[56]|45|76)\\d{8}$" ;
     /**
      * 中国电信:China Telecom
      * 133/153/180/181/189/177
      */
     NSString  *CT = @ "^1((33|53|77|8[019])[0-9]|349)\\d{7}$" ;
     
     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 ;
     }
}
 
/*邮箱验证 */
+ ( BOOL )isValidateEmail:( NSString  *)email {
     NSString  *emailRegex = @ "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" ;
     NSPredicate  *emailTest =
     [ NSPredicate  predicateWithFormat:@ "SELF MATCHES %@" , emailRegex];
     return  [emailTest evaluateWithObject:email];
}

本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4673760.html,如需转载请自行联系原作者

原文链接:https://yq.aliyun.com/articles/363038
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章