用正则表达式验证邮箱和手机号



正则表达式,又称正规表示法、常规表示法(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。

做iOS开发的童鞋,应该都用过正则表达式吧。正则表达式很好地,帮助我们判断一个字符串是否合法。比如:

  1. 在做app注册页面的时候,需要判断手机号是否格式正确,是否够11位。

  2. 在做意见反馈的时候,需要判断邮箱格式是否正确。

判断手机号和判断邮箱,应该是iOS开发者们最常用的。具体如何判断,简单整理如下:

验证邮箱

+ (BOOL)validateEmail:(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];
}

nspredicate是一个Foundation类,是用来查询的,原理和用法都类似于sql中的where。

最初的梦想 范玮琪 - Faces Of FanFan

验证手机号

简单的判断方法

+ (BOOL)validatePhone:(Nsstring *)phone
{
    Nsstring *phoneRegex = @"1[3|5|7|8|][0-9]{9}";
    nspredicate *phoneTest = [nspredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:phone];
}

这只是简单地判断手机号格式。其实手机的格式还是有一点复杂的。

详细的判断方法

//正则判断手机号码格式
+ (BOOL)validatePhone:(Nsstring *)phone
{
       /**
        * 手机号码
        * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
        * 联通:130,131,132,152,155,156,185,186
        * 电信:133,1349,153,180,189
        */
       Nsstring * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
       /**
        10         * 中国移动:China Mobile
        11         * 134[0-8],188
        12         */
       Nsstring * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
       /**
        15         * 中国联通:China Unicom
        16         * 130,186
        17         */
       Nsstring * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
       /**
        20         * 中国电信:China Telecom
        21         * 133,189
        22         */
       Nsstring * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";
       /**
        25         * 大陆地区固话及小灵通
        26         * 区号:010,020,021,022,023,024,025,027,028,029
        27         * 号码:七位或八位
        28         */
      // 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];

    if (([regextestmobile evaluateWithObject:phone] == YES)
    || ([regextestcm evaluateWithObject:phone] == YES)
    || ([regextestct evaluateWithObject:phone] == YES)
    || ([regextestcu evaluateWithObject:phone] == YES))
    {
        if([regextestcm evaluateWithObject:phone] == YES) {
          NSLog(@"China Mobile");
        } else if([regextestct evaluateWithObject:phone] == YES) {
          NSLog(@"China Telecom");
        } else if ([regextestcu evaluateWithObject:phone] == YES) {
          NSLog(@"China Unicom");
        } else {
          NSLog(@"UnkNow");
        }

        return YES;
    }
    else 
    {
        return NO;
    }
}

以上这段(来自网络)是手机号判断的详细方法。基本上这个判断就够用了,如果三大运营商,再出其他段的手机号。直接子啊上面做简单修改即可。

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...