美文网首页
手机号/邮箱的正则

手机号/邮箱的正则

作者: LLL_0901 | 来源:发表于2016-05-03 15:30 被阅读41次

.h

@property (nonatomic,strong)NSString *email;

@property (nonatomic,strong)NSString *mobile;

- (BOOL) validateEmail:(NSString *)email;

- (BOOL) validateMobile:(NSString *)mobile;


.m

//邮箱

- (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];

}

//手机号码验证

- (BOOL) validateMobile:(NSString *)mobile

{

//手机号以13, 15,18开头,八个 \d 数字字符

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

NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];

return [phoneTest evaluateWithObject:mobile];

}

相关文章

网友评论

      本文标题:手机号/邮箱的正则

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