iOS 密码强度判断

2019-07-10  本文已影响0人  旅途开发者

判断密码是否包含字母,数字以及其他字符

+(BOOL)isStringContainNumberWith:(NSString *)str{
    //数字条件
    NSRegularExpression *tNumRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:NSRegularExpressionCaseInsensitive error:nil];
    //符合数字条件的有几个字节
    NSUInteger tNumMatchCount = [tNumRegularExpression numberOfMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)];
    //英文字条件
    NSRegularExpression *tLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z]" options:NSRegularExpressionCaseInsensitive error:nil];
    //符合英文字条件的有几个字节
    NSUInteger tLetterMatchCount = [tLetterRegularExpression numberOfMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)];
    
    if (tNumMatchCount == str.length) {
        NSLog(@"全部数字,沒有英文");
        return NO;
    } else if (tLetterMatchCount == str.length) {
        NSLog(@"全部英文,沒有数字");
        return NO;
    } else if (tNumMatchCount + tLetterMatchCount == str.length) {
        NSLog(@"英文和符合数字条件的相加等于密码长度,达到密码强度最低要求");
        return YES;
    } else {
        NSLog(@"可能包含标点符号的情況,或是包含非英文的文字");
        return YES;
    }
}
上一篇下一篇

猜你喜欢

热点阅读