各种正则相关

前端之家收集整理的这篇文章主要介绍了各种正则相关前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

身份证号码验证正则表达式

需要一个简单的验证身份证号的正则表达式,从网上查了很多,都有问题,主要是只要超过15位,都能够通过,于是自己查了半天手册,写了下面这个式子,能够满足简单的要求:
1、15位或18位,如果是15位,必需全是数字。
2、如果是18位,最后一位可以是数字或字母Xx,其余必需是数字。

/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/

从这个表达式想到的:
1、正则表达式首先是要通过正确的
2、限制不正确
3、可以不限制其中的某此规则,但一定要让正确的先通过。



//邮箱

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

}

//车牌号验证

+ (BOOL) validateCarNo:(NSString *)carNo

{

NSString *carRegex = @"^[/u4e00-/u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_/u4e00-/u9fa5]$";

NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];

NSLog(@"carTest is %@",carTest);

return [carTest evaluateWithObject:carNo];

}

//车型

+ (BOOL) validateCarType:(NSString *)CarType

{

NSString *CarTypeRegex = @"^[/u4E00-/u9FFF]+$";

NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];

return [carTest evaluateWithObject:CarType];

}

//用户名

+ (BOOL) validateUserName:(NSString *)name

{

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

NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];

BOOL B = [userNamePredicate evaluateWithObject:name];

return B;

}

//密码

+ (BOOL) validatePassword:(NSString *)passWord

{

NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";

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

return [passWordPredicate evaluateWithObject:passWord];

}

//昵称

+ (BOOL) validateNickname:(NSString *)nickname

{

NSString *nicknameRegex = @"^[/u4e00-/u9fa5]{4,8}$";

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

return [passWordPredicate evaluateWithObject:nickname];

}

//身份证

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

}

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