谓词(检测文本的正确性)

2016-04-28  本文已影响0人  水晶兰

#define JJPhoneNum @"1[3|5|7|8|][0-9]{9}"  //电话号码

- (void)viewDidLoad {

[super viewDidLoad];

NSString * phoneNum =  @"157188203788";

NSString * phoneNums = @"15718820378";

NSLog(@"1---%@",[self valiateEnterStr:phoneNum byProvisions:JJPhoneNum]?@"yes":@"no");

NSLog(@"2---%@",[self valiateEnterStr:phoneNums byProvisions:JJPhoneNum]?@"yes":@"no");

}

//方法

- (BOOL)valiateEnterStr:(NSString * )enterString byProvisions:(NSString * )provisions{

NSString * regex = provisions;

NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

return [pred evaluateWithObject:enterString];

}

上一篇下一篇

猜你喜欢

热点阅读