+ (NSString*)isOrNoPasswordStyle:(NSString *)passWordName
{
NSString * message;
if (passWordName.length<8) {
message=@" 密码不能少于8位,请您重新输入";
}
else if (passWordName.length>20)
{
message=@"密码最大长度为20位,请您重新输入";
}
else if ([selfJudgeTheillegalCharacter:passWordName])
{
message=@"密码不能包含特殊字符,请您重新输入";
}
else if (![selfjudgePassWordLegal:passWordName])
{
message=@"密码必须同时包含字母和数字";
}
return message;
}
+(BOOL)JudgeTheillegalCharacter:(NSString *)content{
//提示标签不能输入特殊字符
NSString *str =@"^[A-Za-z0-9\\u4e00-\u9fa5]+$";
NSPredicate* emailTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", str];
if (![emailTestevaluateWithObject:content]) {
returnYES;
}
returnNO;
}
+(BOOL)judgePassWordLegal:(NSString *)pass{
BOOL result ;
// 判断长度大于8位后再接着判断是否同时包含数字和大小写字母
NSString * regex =@"(?![0-9A-Z]+$)(?![0-9a-z]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$ ";
NSPredicate *pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", regex];
result = [pred evaluateWithObject:pass];
NSLog(@"%hhd",result);
return result;
}
提示:以返回字符串的长度来判断,长度大于0,证明有返回值,证明不符合规则,直接提示返回字符串便可
正则表达式:
1.8~20位同时包含数字和大小写字母 (?![0-9A-Z]+$)(?![0-9a-z]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$
2.8~20位同时包含数字和字母
^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$