正则表达式(邮箱,密码,手机号,身份证)

前端之家收集整理的这篇文章主要介绍了正则表达式(邮箱,密码,手机号,身份证)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//邮箱

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

{

//手机号以1314,1518开头,八个 \d 数字字符

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

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

return [phoneTest evaluateWithObject:mobile];

}

//密码

- (BOOL) validatePassword:(NSString *)passWord

{

// NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$"; 密码是数字或者字母

NSString *passWordRegex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";//密码是数字或者字母,且不能全部是数字,全部是字母

NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];

return [passWordPredicate evaluateWithObject:passWord];

}

//身份证号

- (BOOL) validateIdentityCard: (NSString *)identityCard

{

BOOL flag;

if (identityCard.length <= 0) {

flag = NO;

return flag;

}

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

NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];

return [identityCardPredicate evaluateWithObject:identityCard];

}

猜你在找的正则表达式相关文章