正则表达式 手机号验证

#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(@"不正确");

}


}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容