字符串验证

2020-12-10  本文已影响0人  EnjoyWT

密码复杂度验证

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface JoyStringValidater : NSObject
+ (BOOL)validateString:(NSString *)content withLeng:(NSString *)rangeString;
+ (BOOL)validateContainsChinese:(NSString *)content;
+ (BOOL)validateContainsNumber:(NSString *)content;
+ (BOOL)validateContainsSmallLetter:(NSString *)content;
+ (BOOL)validateContainsCapitalLetter:(NSString *)content;
+ (BOOL)validateContainsSpecialCharacter:(NSString *)content;
+ (BOOL)validateContainsWhitespaces:(NSString *)content;
@end

NS_ASSUME_NONNULL_END

#import "JoyStringValidater.h"

@implementation JoyStringValidater

#pragma --mark 字符串长度长度默认8~30
+ (BOOL)validateString:(NSString *)content withLeng:(NSString *)rangeString{
    
    NSString *regexString=@"^[\\x21-\\x7e]{8,30}$";
    if (regexString) {
        regexString=[NSString stringWithFormat:@"^[\\x21-\\x7e]%@$",rangeString];
    }
    
     return [self validateNSPredicate:regexString withContent:content];;
   
}
#pragma --mark 是否含有中文
+ (BOOL)validateContainsChinese:(NSString *)content
{
    NSRegularExpression *regularexpression = [[NSRegularExpression alloc]
                                              initWithPattern:@"^[\u4e00-\u9fa5]"
                                              options:NSRegularExpressionCaseInsensitive
                                              error:nil];
    return ([regularexpression numberOfMatchesInString:content
                                               options:NSMatchingReportProgress
                                               range:NSMakeRange(0, content.length)] > 0);
}
#pragma --mark 是否含有数字
+ (BOOL)validateContainsNumber:(NSString *)content{
    
    NSString *regexString=@".*[0-9]+.*";
  
    return [self validateNSPredicate:regexString withContent:content];;
}
#pragma --mark 是否含有小写字母
+ (BOOL)validateContainsSmallLetter:(NSString *)content{
    
    NSString *regexString= @".*[a-z]+.*";
  
   return [self validateNSPredicate:regexString withContent:content];;
   
}
#pragma --mark 是否含有大写字母
+ (BOOL)validateContainsCapitalLetter:(NSString *)content{
    
    NSString *regexString= @".*[A-Z]+.*";
  
    return [self validateNSPredicate:regexString withContent:content];;
   
}
#pragma --mark 是否含有特殊字符
+ (BOOL)validateContainsSpecialCharacter:(NSString *)content{
    
    NSString *regexString=  @".*[\\x21-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7B-\\x7F]+.*";
  
     return [self validateNSPredicate:regexString withContent:content];;
   
}
#pragma --mark 是否含有空格
+ (BOOL)validateContainsWhitespaces:(NSString *)content{
    
    NSCharacterSet *charSet = [NSCharacterSet whitespaceCharacterSet];
    NSRange _range = [content rangeOfCharacterFromSet:charSet];
    return _range.location !=NSNotFound;
   
}
+(BOOL)validateNSPredicate:(NSString *)regexString withContent:(NSString *)content {
    
    if (!regexString ||!content) {
        return false;
    }
    NSPredicate *stringPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regexString];
       
    return [stringPredicate evaluateWithObject:content];
}

@end

上一篇下一篇

猜你喜欢

热点阅读