iOS密码等级验证效果如图 ,原理是判断是否有数字,字母特殊字符来区别等级显示
-(NSString *)blackClassLblText:(NSString*)str
{
NSMutableArray* resultArray = [[NSMutableArrayalloc] init];
NSArray* termArray1 = [[NSArrayalloc] initWithObjects:@"a",@"b", @"c",@"d", @"e",@"f", @"g",@"h", @"i",@"j", @"k",@"l", @"m",@"n", @"o",@"p", @"q",@"r", @"s",@"t", @"u",@"v", @"w",@"x", @"y",@"z", nil];
NSArray* termArray2 = [[NSArrayalloc] initWithObjects:@"1",@"2", @"3",@"4", @"5",@"6", @"7",@"8", @"9",@"0", nil];
NSArray* termArray3 = [[NSArrayalloc] initWithObjects:@"A",@"B", @"C",@"D", @"E",@"F", @"G",@"H", @"I",@"J", @"K",@"L", @"M",@"N", @"O",@"P", @"Q",@"R", @"S",@"T", @"U",@"V", @"W",@"X", @"Y",@"Z", nil];
NSArray* termArray4 = [[NSArrayalloc] initWithObjects:@"~",@"`",@"@",@"#",@"$",@"%",@"^",@"&",@"*",@"(",@")",@"-",@"_",@"+",@"=",@"{",@"}",@"[",@"]",@"|",@":",@";",@"“",@"'",@"‘",@"<",@",",@".",@">",@"?",@"/",@"、",nil];
NSString* result1 = [NSStringstringWithFormat:@"%d",[selfjudgeRange:termArray1 Password:str]];
NSString* result2 = [NSStringstringWithFormat:@"%d",[selfjudgeRange:termArray2 Password:str]];
NSString* result3 = [NSStringstringWithFormat:@"%d",[selfjudgeRange:termArray3 Password:str]];
NSString* result4 = [NSStringstringWithFormat:@"%d",[selfjudgeRange:termArray4 Password:str]];
[resultArray addObject:[NSStringstringWithFormat:@"%@",result1]];
[resultArray addObject:[NSStringstringWithFormat:@"%@",result2]];
[resultArray addObject:[NSStringstringWithFormat:@"%@",result3]];
[resultArray addObject:[NSStringstringWithFormat:@"%@",result4]];
int intResult=0;
for (int j=0; j<[resultArraycount]; j++)
{
if ([[resultArrayobjectAtIndex:j] isEqualToString:@"1"])
{
intResult++;
}
}
NSString* resultString = [[NSStringalloc] init];
if (intResult <2)
{
resultString = @"低级";
}
elseif (intResult == 2)
{
resultString = @"中级";
}
if (intResult >2)
{
resultString = @"高级";
}
return resultString;
}
-(BOOL)judgeRange:(NSArray*) _termArray Password:(NSString*) _password
{
NSRange range;
BOOL result =NO;
for(int i=0; i<[_termArraycount]; i++)
{
range = [_password rangeOfString:[_termArrayobjectAtIndex:i]];
if(range.location !=NSNotFound)
{
result =YES;
}
}
return result;
}
//代理里边控制
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField==self.phoneTextfield) {
self.classLbl2.hidden=NO;
self.classLbl2.text=[selfblackClassLblText:self.phoneTextfield.text];
if ([self.classLbl2.textisEqualToString:@"低级"]) {
//设置字体颜色RGBA(243, 60, 62, 1) 为宏定义
self.classLbl2.textColor=RGBA(243,60, 62,1);
}else if ([self.classLbl2.textisEqualToString:@"中级"])
{
self.classLbl2.textColor=RGBA(255,185, 42,1);
}else
{
self.classLbl2.textColor=RGBA(41,184, 27,1);
}
}
return YES;
}