iOS之密码判断

不多比比,直接贴上封装好的分类!
NSString+UTStringJudgement.h文件

//
//  NSString+UTStringJudgement.h
//  UtecPublicClass
//
//  Created by 贾书唱的macbook on 2019/4/16.
//  Copyright © 2019 U-tec. All rights reserved.
//

#import 

NS_ASSUME_NONNULL_BEGIN

@interface NSString (UTStringJudgement)
/**
 * @jsc_brief 返回密码判断字符串:6-18位,不包含特殊字符,必须有字母和数字
 *
 * @jsc_param passWordName 密码
 *
 * @jsc_return 返回一个字符串描述密码判断之后的文字
 */
+ (NSString *)isOrNoPasswordStyle:(NSString *)passWordName;
/**
 * @jsc_brief 返回密码判断成功和失败:返回密码判断字符串:8-16位,不包含特殊字符,必须有大小写字母和数字
 *
 * @jsc_param passWordName 密码
 *
 * @jsc_return 返回一个bool值,描述成功和失败
 */
+ (BOOL)isRightPSD:(NSString *)passWordName;
@end

NS_ASSUME_NONNULL_END

NSString+UTStringJudgement.m文件

//
//  NSString+UTStringJudgement.m
//  UtecPublicClass
//
//  Created by 贾书唱的macbook on 2019/4/16.
//  Copyright © 2019 U-tec. All rights reserved.
//
//正则表达式:
//
//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}$
//
//3.6~18位同时包含数字和字母  ^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$
//
//4.6~18位同时包含数字和大写字母  ^(?![0-9]+$)(?![a-zA-Z]+$)(?![a-z0-9]+$)[0-9A-Za-z]{6,18}$


#import "NSString+UTStringJudgement.h"

@implementation NSString (UTStringJudgement)

#pragma mark - 返回密码判断字符串
+ (NSString *)isOrNoPasswordStyle:(NSString *)passWordName

{
    
    NSString * message;
    
    if (passWordName.length < 6) {
        
        message = @"密码不能少于6位,请您重新输入";
        
    }
    
    else if (passWordName.length>18)
        
    {
        
        message = @"密码最大长度为18位,请您重新输入";
        
    }
    
    else if ([self JudgeTheillegalCharacter:passWordName])
        
    {
        
        message = @"密码不能包含特殊字符,请您重新输入";
        
    }
    
    else if (![self judgePassWordLegal:passWordName])
        
    {
        
        message = @"密码必须同时包含字母和数字";
        
    }else{
        message = @"密码格式正确";
    }
    
    return message;
    
}

#pragma mark - 返回密码判断成功和失败
+ (BOOL)isRightPSD:(NSString *)passWordName{
    BOOL result = YES;
    
    if ([self JudgeTheillegalCharacter:passWordName]) {
        //包含特殊字符
        result = NO;
    }else if (![self judgePassWord:passWordName]){
        //不符合密码设定
        result = NO;
    }
    
    return result;
}

#pragma mark - 特殊字符判断
+ (BOOL)JudgeTheillegalCharacter:(NSString *)content{
    
    //提示标签不能输入特殊字符
    
    NSString *str =@"^[A-Za-z0-9\\u4e00-\u9fa5]+$";
    
    NSPredicate* emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    
    if (![emailTest evaluateWithObject:content]) {
        
        return YES;
        
    }
    
    return NO;
    
}

#pragma mark - 6-18位正则表达式,必须包含数字和字母
+ (BOOL)judgePassWordLegal:(NSString *)pass{
    
    BOOL result ;
    
    // 判断长度大于6位后再接着判断是否同时包含数字和字母
    NSString * regex =@"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    result = [pred evaluateWithObject:pass];
    
    return result;
    
}
#pragma mark - 8-16位正则表达式,必须包含数字和大小写字母
+ (BOOL)judgePassWord:(NSString *)pass{
    
    BOOL result ;
    
    NSString * regex =@"(?![0-9A-Z]+$)(?![0-9a-z]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    result = [pred evaluateWithObject:pass];
    
    return result;
    
}
@end


你可能感兴趣的:(iOS之密码判断)