美文网首页
正则表达式 手机号验证

正则表达式 手机号验证

作者: 9898a | 来源:发表于2017-06-30 10:29 被阅读0次

    #import "ViewController.h"

    @interface ViewController ()

    @property (weak, nonatomic) IBOutlet UITextField *phoneTF;

    @end

    @implementation ViewController

    - (IBAction)BUTMessage:(id)sender {

    NSString *phone = self.phoneTF.text;

    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0-9])|(14[57])|(17[013678]))\\d{8}$";

    NSPredicate *preURL = [NSPredicate predicateWithFormat:@"self matches%@",phoneRegex];

    BOOL b1 = [preURL evaluateWithObject:phone];

    if (b1) {

    NSLog(@"正确");

    }else{

    NSLog(@"不正确");

    }

    - (IBAction)SFZBUT:(id)sender {

    NSString *phone = self.phoneTF.text;

    NSString *SfZRegex = @"^(\\d{14}|\\d{17})(\\d|[xX])$";

    NSPredicate *preURL = [NSPredicate predicateWithFormat:@"self matches%@",SfZRegex];

    BOOL b1 = [preURL evaluateWithObject:phone];

    if (b1) {

    NSLog(@"正确");

    }else{

    NSLog(@"不正确");

    }

    }

    - (IBAction)YXBUT:(id)sender {

    NSString *phone = self.phoneTF.text;

    NSString *YXRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

    NSPredicate *preURL = [NSPredicate predicateWithFormat:@"self matches%@",YXRegex];

    BOOL b1 = [preURL evaluateWithObject:phone];

    if (b1) {

    NSLog(@"正确");

    }else{

    NSLog(@"不正确");

    }

    }

    - (IBAction)YM:(id)sender {

    NSString *phone = self.phoneTF.text;

    NSString *YHMRegex = @"^[A-Za-z0-9]{6,20}+$";

    NSPredicate *preURL = [NSPredicate predicateWithFormat:@"self matches%@",YHMRegex];

    BOOL b1 = [preURL evaluateWithObject:phone];

    if (b1) {

    NSLog(@"正确");

    }else{

    NSLog(@"不正确");

    }

    }

    相关文章

      网友评论

          本文标题:正则表达式 手机号验证

          本文链接:https://www.haomeiwen.com/subject/zpybcxtx.html