iOS 项目日志 -- 密码强度 实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 项目日志 -- 密码强度 实现相关的知识,希望对你有一定的参考价值。

没有采用Predicate 直接用的是bool值做的

代码如下

@interface ViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIProgressView *levelProgress;

@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;

@property (weak, nonatomic) IBOutlet UITextField *confirmPwdTextField;

@property (weak, nonatomic) IBOutlet UILabel *levelLabel;

 

@end

 

@implementation ViewController

 

-(BOOL)judgeWithArray:(NSArray *)arr withPwd:(NSString *)pwd

{

    NSRange range;

    for (int i =0; i<arr.count; i++) {

        range = [pwd rangeOfString:arr[i]];

        

        if (range.location != NSNotFound) {

            return YES;

        }

    }

    return NO;

 

}

 

-(void)textFieldDidEndEditing:(UITextField *)textField{

    [self.pwdTextField becomeFirstResponder];

//    NSLog(@"%d",self.pwdTextField.isEditing);

    NSArray* termArray1 = [[NSArray alloc] 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 = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"0", nil];

    NSArray* termArray3 = [[NSArray alloc] 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 = [[NSArray alloc] initWithObjects:@"~",@"`",@"@",@"#",@"$",@"%",@"^",@"&",@"*",@"(",@")",@"-",@"_",@"+",@"=",@"{",@"}",@"[",@"]",@"|",@":",@";",@"“",@"‘",@"‘",@"<",@",",@".",@">",@"?",@"/",@"、", nil];

    

    

    NSString *strResult1 = [NSString stringWithFormat:@"%d",[self judgeWithArray:termArray1 withPwd:self.pwdTextField.text]];

    NSString *strResult2 = [NSString stringWithFormat:@"%d",[self judgeWithArray:termArray2 withPwd:self.pwdTextField.text]];

    NSString *strResult3 = [NSString stringWithFormat:@"%d",[self judgeWithArray:termArray3 withPwd:self.pwdTextField.text]];

    NSString *strResult4 = [NSString stringWithFormat:@"%d",[self judgeWithArray:termArray4 withPwd:self.pwdTextField.text]];

    

    NSMutableArray *arrResult = [[NSMutableArray alloc]init];

    

    [arrResult addObject:strResult1];

    [arrResult addObject:strResult2];

    [arrResult addObject:strResult3];

    [arrResult addObject:strResult4];

    

    

    int intResult = 0;

    for (int j = 0; j<arrResult.count; j++) {

        if ([arrResult[j] isEqualToString:@"1"]) {

            intResult ++;

        }

    }

    

    if (intResult <2) {

        self.levelLabel.text = @"弱";

        self.levelProgress.progress = 0.2;

        self.levelProgress.progressTintColor = [UIColor redColor];

    }else if(intResult == 2){

        self.levelLabel.text = @"中";

        self.levelProgress.progressTintColor = [UIColor yellowColor];

        self.levelProgress.progress = 0.6;

    }else if (intResult >2){

        self.levelProgress.progressTintColor = [UIColor greenColor];

        self.levelProgress.progress = 0.9;

        self.levelLabel.text = @"强";

    }

    

 

}

- (void)viewDidLoad {

    [super viewDidLoad];

 

    self.pwdTextField.delegate = self;

    

    

}

 

 

技术分享

以上是关于iOS 项目日志 -- 密码强度 实现的主要内容,如果未能解决你的问题,请参考以下文章

利用JavaScript来实现用动态检验密码强度

密码强度实现 ExtJS

注册时的密码强度

React + TS 封装密码强度组件

AngularJS 实现判断密码

密码强度的正则表达式(JavaScript)总结