美文网首页
iOS | 小笔记:常用方法之校验类

iOS | 小笔记:常用方法之校验类

作者: 佳小豆 | 来源:发表于2018-07-17 15:57 被阅读16次
    • 11位手机号
    + (BOOL)isValidPhone:(NSString *)originalStr
    {
        NSString *phoneRegex = @"^1([3456789])\\d{9}";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
        return [pred evaluateWithObject:originalStr];
    }
    
    • 邮箱
    + (BOOL)isValidEmail:(NSString *)originalStr
    {
        NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
        return [pred evaluateWithObject: originalStr];
    }
    
    • 身份证号(目前是15位和18位)
    + (BOOL)isValidIDCardNum:(NSString *)originalStr
    {
        BOOL isMatch = NO;
        
        if ([self isBlankString:originalStr])
        {
            return isMatch;
        }
        
        if(originalStr.length!=15&&originalStr.length!=18)
        {
            return isMatch;
        }
        
        NSString *cardNo = [NSString stringWithFormat:@"%@",originalStr];
        
        NSString *idCodeRegex = @"((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])| (5[0-4])|(6[1-5])|71|(8[12])|91)";
        NSPredicate *idCodeTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", idCodeRegex];
        
        if (![idCodeTest evaluateWithObject:[cardNo substringToIndex:2]])
        {
            return isMatch;
        }
        
        NSArray *strJiaoYan = [NSArray arrayWithObjects:@"1", @"0", @"X", @"9", @"8", @"7", @"6", @"5",@"4", @"3", @"2", nil];
        NSArray *arr = [NSArray arrayWithObjects:@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2", nil];
        
        if (cardNo.length == 15)
        {
            NSString *ereg = @"";
            
            if (([[cardNo substringWithRange:NSMakeRange(6, 2)]intValue]+1900)%4 == 0
                || (([[cardNo substringWithRange:NSMakeRange(6, 2)]intValue]+1900)%100 == 0
                    && ([[cardNo substringWithRange:NSMakeRange(6, 2)]intValue]+1900)%4 == 0))
            {
                // 测试出生日期的合法性
                ereg = @"[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}";
            }
            else
            {
                // 测试出生日期的合法性
                ereg = @"[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}";
            }
            
            
            NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",ereg];
            isMatch = [pred evaluateWithObject:cardNo];
            if (isMatch == NO)
            {
                return isMatch;
            }
            isMatch = NO;
            cardNo = [NSString stringWithFormat:@"%@19%@",[cardNo substringToIndex:6],[cardNo substringWithRange:NSMakeRange(6, 9)]];
            
            int intTemp = 0;
            
            for (int i = 0; i<17; i++)
            {
                intTemp = intTemp + ([[cardNo substringWithRange:NSMakeRange(i, 1)]intValue] * [[arr objectAtIndex:i]intValue]);
            }
            
            intTemp = intTemp % 11;
            cardNo = [NSString stringWithFormat:@"%@%@",cardNo,[strJiaoYan objectAtIndex:intTemp]];
        }
        if (cardNo.length == 18)
        {
            NSString *ereg1 = @"";
            if (([[cardNo substringWithRange:NSMakeRange(6, 4)]intValue])%4 == 0
                || (([[cardNo substringWithRange:NSMakeRange(6, 4)]intValue])%100 == 0
                    && ([[cardNo substringWithRange:NSMakeRange(6, 4)]intValue])%4 == 0))
            {
                // 闰年出生日期的合法性正则表达式
                ereg1 = @"[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]";
            }
            else
            {
                // 平年出生日期的合法性正则表达式
                ereg1 = @"[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]";
            }
            NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",ereg1];
            isMatch = [pred evaluateWithObject:cardNo];
            if (isMatch == NO)
            {
                return isMatch;
            }
            isMatch = NO;
            
            int intTemp = 0;
            
            for (int i = 0; i<17; i++)
            {
                intTemp = intTemp + ([[cardNo substringWithRange:NSMakeRange(i, 1)]intValue] * [[arr objectAtIndex:i]intValue]);
            }
            
            intTemp = intTemp % 11;
            
            NSString *last = [strJiaoYan objectAtIndex:intTemp];// 判断校验位
            
            if (!([last compare:[cardNo substringWithRange:NSMakeRange(17, 1)] options:NSCaseInsensitiveSearch] == NSOrderedSame))
            {
                return isMatch;
            }
            
        }
        isMatch = YES;
        return isMatch;
    }
    
    • 汉字
    + (BOOL)isChinese:(NSString *)originalStr
    {
        NSString *chineseRegex = @"(^[\u4e00-\u9fa5]+[·•]?[\u4e00-\u9fa5]+$)";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF matches %@", chineseRegex];
        return [pred evaluateWithObject:originalStr];
    }
    
    • 纯数字
    + (BOOL)isPureInt:(NSString*)originalStr
    {
        NSScanner *scan = [NSScanner scannerWithString: originalStr];
        int val;
        return [scan scanInt:&val] && [scan isAtEnd];
    }
    
    • 空字符串
    + (BOOL)isBlankString:(NSString*)originalStr
    {
        if([originalStr isKindOfClass:[NSNull class]])
        {
            return YES;
        }
        if([originalStr isEqual:[NSNull null]])
        {
            return YES;
        }
        if (originalStr == nil)
        {
            return YES;
        }
        if([originalStr isEqualToString:@""] || [originalStr isEqualToString:@"null"] || [originalStr isEqualToString:@"<null>"])
        {
            return YES;
        }
        if([originalStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length == 0)
        {
            return YES;
        }
        return NO;
    }
    
    • 是否包含特殊符号
    + (BOOL)hasIllegalChar:(NSString *)originalStr
    {
       //此集合中写入不想在字符串中出现的字符,视情况更改
        NSCharacterSet *illegalCharStr = [NSCharacterSet characterSetWithCharactersInString:@"\\/:*?\"<|'%>&@#"];
        NSRange range = [originalStr rangeOfCharacterFromSet:illegalCharStr];
        return range.location < originalStr.length;
    }
    

    相关文章

      网友评论

          本文标题:iOS | 小笔记:常用方法之校验类

          本文链接:https://www.haomeiwen.com/subject/smwrjftx.html