最新的正则效验效验手机号,邮箱,qq,修复网上的bug

最新的正则效验效验手机号,邮箱,qq,修复网上的bug_第1张图片
项目截图

最近项目需求,意见反馈里面有一个联系方式,里面可以输入手机号,qq号码,邮箱等,需要判断格式是否正确,网上百度了一下,正则效验比较好用,但是网上的方法有bug,电信的号码173的号码没有判断,用网上的方法会报错,所以就网上的方法修改了一下,现在应该这个是最新的方法了!!!!话不多说,上代码!!!

1.设置三个状态

//邮箱
@property (nonatomic, assign)BOOL emailRight;
//手机
@property (nonatomic, assign)BOOL phoneRight;
//QQ
@property (nonatomic, assign)BOOL qqRight;

2.验证手机号码的格式

 /**
     * 手机号码
     * 移动: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],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";
    /**
     15         * 中国联通:China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1((33|53|77|73|8[09])[0-9]|349)\\d{7}$";;
    /**
     20         * 中国电信:China Telecom
     21         * 133,1349,153,180,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:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        self.phoneRight = 1;
        return YES;
    }
    else
    {
        self.phoneRight = 0;
        return NO;
    }

2.验证邮箱的格式

-(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];
    self.emailRight = [emailTest evaluateWithObject:email];
    return self.emailRight;    
}

3.验证qq号码的格式

#pragma mark 判断qq
-(BOOL)isValidateqq:(NSString *)qq{
    
    NSString *qqRegex = @"[1-9][0-9]{4,14}";
    NSPredicate *qqlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", qqRegex];
    self.qqRight = [qqlTest evaluateWithObject:qq];
    return self.qqRight;
    }

4.在点击的地方触发传入值

- (IBAction)tijiao:(UIButton *)sender {
    
    //判断输入是否为空
    if (self.feedTextView.text.length == 0) {
        [self showAlertWithMessage:@"你输入的信息为空,请重新输入"];
    }
    else{
        [self isMobileNumber:self.contactTextField.text];
        [self isValidateEmail:self.contactTextField.text];
        
        [self  isValidateqq:self.contactTextField.text];
        //验证qq未写
        
        if (self.emailRight != 0 || self.phoneRight != 0 || self.qqRight != 0) {
            //传给后台
            [self  url];
   
        }
        else{
    
            [self TishishowMessage:@"联系方式格式错误" duration:2]; 
        }
    }

}

你可能感兴趣的:(最新的正则效验效验手机号,邮箱,qq,修复网上的bug)