美文网首页
IOS基础:常用的格式校验方法

IOS基础:常用的格式校验方法

作者: 时光啊混蛋_97boy | 来源:发表于2020-11-03 14:43 被阅读0次

    原创:知识点总结性文章
    创作不易,请珍惜,之后会持续更新,不断完善
    个人比较喜欢做笔记和写总结,毕竟好记性不如烂笔头哈哈,这些文章记录了我的IOS成长历程,希望能与大家一起进步
    温馨提示:由于简书不支持目录跳转,大家可通过command + F 输入目录标题后迅速寻找到你所需要的内容

    目录

    • 一、数字格式校验
      • 判断文本框字数是否符合规范
      • 判断验证码是否正确
      • 判断输入金额是否正确
      • 判断是否是手机号
      • 判断是否是移动号
      • 判断是否是联通号
      • 判断是否是电信号
      • 18位身份证格式和合法性验证
      • 车牌号格式校验(粤A8888澳)
      • 银行卡格式验证
    • 二、混合校验
      • 6~16位数字/字母/下划线组成的密码格式校验
      • 判断字符串是否全为[(数字)、(字母)、(数字|字母)、(汉字)]
      • 邮箱格式验证
      • 判断密码格式是否正确
    • 三、文字校验
      • 昵称
    • 四、文本框校验
      • 输入判断小数点后两位
    • Demo
    • 参考文献

    一、数字格式校验

    判断文本框字数是否符合规范

    /** 判断文本框字数是否符合规范 */
    + (BOOL)validateTextCount:(NSString *)text;
    
    // 判断文本框字数是否符合规范
    + (BOOL)validateTextCount:(NSString *)text
    {
        NSString * regexExpress = @"^.{1,8}$";
        NSPredicate * predicate = [NSPredicate predicateWithFormat: @"SELF MATCHES %@", regexExpress];
        return [predicate evaluateWithObject: text];
    }
    

    输出结果为:

    2020-09-27 10:52:46.477566+0800 FunctionCodeBlockDemo[18729:18850103] 待校验的字符串为:1234567
    2020-09-27 10:52:46.479235+0800 FunctionCodeBlockDemo[18729:18850103] 格式正确
    2020-09-27 10:53:51.828923+0800 FunctionCodeBlockDemo[18772:18852912] 待校验的字符串为:123456789
    2020-09-27 10:53:51.830487+0800 FunctionCodeBlockDemo[18772:18852912] 格式错误
    

    判断验证码是否正确

    /** 判断验证码是否正确 */
    + (BOOL)validateAuthen:(NSString *)text;
    
    // 判断验证码是否正确
    + (BOOL)validateAuthen:(NSString *)text
    {
        NSString * regexExpress = @"^\\d{6}$";
        NSPredicate * predicate = [NSPredicate predicateWithFormat: @"SELF MATCHES %@", regexExpress];
        return [predicate evaluateWithObject: text];
    }
    

    输出结果为:

    2020-09-27 10:56:37.743354+0800 FunctionCodeBlockDemo[18824:18855832] 待校验的字符串为:123456
    2020-09-27 10:56:37.744975+0800 FunctionCodeBlockDemo[18824:18855832] 格式正确
    2020-09-27 10:56:48.175096+0800 FunctionCodeBlockDemo[18834:18856731] 待校验的字符串为:12345
    2020-09-27 10:56:48.176840+0800 FunctionCodeBlockDemo[18834:18856731] 格式错误
    

    判断输入金额是否正确

    /** 判断输入金额是否正确 */
    + (BOOL)validateMoney:(NSString *)text
    
    // 判断输入金额是否正确
    + (BOOL)validateMoney:(NSString *)text
    {
        NSString * regexExpress = @"^\\d{1,}$";
        NSPredicate * predicate = [NSPredicate predicateWithFormat: @"SELF MATCHES %@", regexExpress];
        return [predicate evaluateWithObject: text];
    }
    

    输出结果为:

    2020-09-27 10:58:21.199859+0800 FunctionCodeBlockDemo[18865:18858669] 待校验的字符串为:12.5
    2020-09-27 10:58:21.201540+0800 FunctionCodeBlockDemo[18865:18858669] 格式错误
    2020-09-27 10:58:48.960328+0800 FunctionCodeBlockDemo[18883:18859971] 待校验的字符串为:12
    2020-09-27 10:58:48.962059+0800 FunctionCodeBlockDemo[18883:18859971] 格式正确
    2020-09-27 10:59:00.854211+0800 FunctionCodeBlockDemo[18896:18860861] 待校验的字符串为:
    2020-09-27 10:59:00.855838+0800 FunctionCodeBlockDemo[18896:18860861] 格式错误
    

    判断是否是手机号

    /** 判断是否是手机号 */
    + (BOOL)isMobilePhone:(NSString *)phoneNumber;
    
    // 判断是否是手机号
    + (BOOL)isMobilePhone:(NSString *)phoneNumber
    {
        NSString * Mobile = @"^1(3[0-9]|4[579]|5[0-35-9]|7[01356]|8[0-9])\\d{8}$";
        NSPredicate *regexMobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Mobile];
        if ([regexMobile evaluateWithObject:phoneNumber])
        {
            return YES;
        }
        return NO;
    }
    

    输出结果为:

    2020-09-27 11:01:11.720826+0800 FunctionCodeBlockDemo[18936:18863245] 待校验的字符串为:15659281708
    2020-09-27 11:01:11.722522+0800 FunctionCodeBlockDemo[18936:18863245] 格式正确
    2020-09-27 11:01:48.114921+0800 FunctionCodeBlockDemo[18956:18864543] 待校验的字符串为:11111111111
    2020-09-27 11:01:48.116660+0800 FunctionCodeBlockDemo[18956:18864543] 格式错误
    

    判断是否是移动号

    /** 判断是否是移动号 */
    + (BOOL)isCMMobilePhone:(NSString *)phoneNumber;
    
    // 判断是否是移动号
    + (BOOL)isCMMobilePhone:(NSString *)phoneNumber
    {
        NSString * CM = @"^1(34[0-8]|70[356]|(3[5-9]|4[7]|5[0-27-9]|7[8]|8[2-47-8])\\d)\\d{7}$";
        NSPredicate *regexTestCM = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
        if ([regexTestCM evaluateWithObject:phoneNumber])
        {
            return YES;
        }
        return NO;
    }
    

    输出结果为:

    2020-09-27 11:20:23.235448+0800 FunctionCodeBlockDemo[19152:18874471] 待校验的字符串为:17859733953
    2020-09-27 11:20:23.237178+0800 FunctionCodeBlockDemo[19152:18874471] 格式正确
    2020-09-27 11:20:39.546122+0800 FunctionCodeBlockDemo[19165:18877042] 待校验的字符串为:15659281708
    2020-09-27 11:20:39.547760+0800 FunctionCodeBlockDemo[19165:18877042] 格式错误
    

    判断是否是联通号

    /** 判断是否是联通号 */
    + (BOOL)isCUMobilePhone:(NSString *)phoneNumber;
    
    // 判断是否是联通号
    + (BOOL)isCUMobilePhone:(NSString *)phoneNumber
    {
        NSString * CU = @"^1(70[07-9]|(3[0-2]|4[5]|5[5-6]|7[15-6]|8[5-6])\\d)\\d{7}$";
        NSPredicate *regexTestCU = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
        if ([regexTestCU evaluateWithObject:phoneNumber])
        {
            return YES;
        }
        return NO;
    }
    

    输出结果为:

    2020-09-27 11:22:36.249312+0800 FunctionCodeBlockDemo[19238:18880954] 待校验的字符串为:15659281708
    2020-09-27 11:22:36.250896+0800 FunctionCodeBlockDemo[19238:18880954] 格式正确
    2020-09-27 11:22:50.018542+0800 FunctionCodeBlockDemo[19257:18881900] 待校验的字符串为:17859733953
    2020-09-27 11:22:50.020172+0800 FunctionCodeBlockDemo[19257:18881900] 格式错误
    

    判断是否是电信号

    /** 判断是否是电信号 */
    + (BOOL)isCTMobilePhone:(NSString *)phoneNumber;
    
    // 判断是否是电信号
    + (BOOL)isCTMobilePhone:(NSString *)phoneNumber
    {
        NSString * CT = @"^1(34[9]|70[0-2]|(3[3]|4[9]|5[3]|7[37]|8[019])\\d)\\d{7}$";
        NSPredicate *regexTestCT = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
        if ([regexTestCT evaluateWithObject:phoneNumber])
        {
            return YES;
        }
        return NO;
    }
    

    输出结果为:

    2020-09-27 11:24:30.699399+0800 FunctionCodeBlockDemo[19300:18884939] 待校验的字符串为:17338760223
    2020-09-27 11:24:30.701030+0800 FunctionCodeBlockDemo[19300:18884939] 格式正确
    2020-09-27 11:24:47.980577+0800 FunctionCodeBlockDemo[19317:18886002] 待校验的字符串为:17859733953
    2020-09-27 11:24:47.982231+0800 FunctionCodeBlockDemo[19317:18886002] 格式错误
    2020-09-27 11:24:59.420994+0800 FunctionCodeBlockDemo[19327:18886842] 待校验的字符串为:15659281708
    2020-09-27 11:24:59.422598+0800 FunctionCodeBlockDemo[19327:18886842] 格式错误
    

    18位身份证格式和合法性验证

    /** 18位身份证格式和合法性验证 */
    +(BOOL)checkUserID:(NSString *)userID;
    
    // 18位身份证格式和合法性验证
    +(BOOL)checkUserID:(NSString *)userID
    {
        // 长度不为18的都排除掉
        if (userID.length != 18)
        {
            return NO;
        }
        
        // 校验格式
        NSString *regex = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
        NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        BOOL flag = [identityCardPredicate evaluateWithObject:userID];
        
        if (!flag)// 格式错误
        {
            return flag;
        }
        else// 格式正确再判断是否合法
        {
            // 将前17位加权因子保存在数组里
            NSArray *idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
            
            // 这是除以11后,可能产生的11位余数、验证码,也保存成数组
            NSArray *idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
            
            // 用来保存前17位各自乖以加权因子后的总和
            NSInteger idCardWiSum = 0;
            for(int i = 0;i < 17;i++)
            {
                NSInteger subStrIndex = [[userID substringWithRange:NSMakeRange(i, 1)] integerValue];
                NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];
                idCardWiSum += subStrIndex * idCardWiIndex;
            }
            
            // 计算出校验码所在数组的位置
            NSInteger idCardMod = idCardWiSum%11;
            
            // 得到最后一位身份证号码
            NSString * idCardLast= [userID substringWithRange:NSMakeRange(17, 1)];
            
            // 如果等于2,则说明校验码是10,身份证号码最后一位应该是X
            if(idCardMod == 2)
            {
                if([idCardLast isEqualToString:@"X"] || [idCardLast isEqualToString:@"x"])
                {
                    return YES;
                }
                else
                {
                    return NO;
                }
            }
            else
            {
                // 用计算出的验证码与最后一位身份证号码匹配,如果一致,说明通过,否则是无效的身份证号码
                if([idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]])
                {
                    return YES;
                }
                else
                {
                    return NO;
                }
            }
        }
    }
    

    输出结果为:

    2020-09-27 11:27:05.324091+0800 FunctionCodeBlockDemo[19364:18889544] 待校验的字符串为:11010119900307643X
    2020-09-27 11:27:05.325713+0800 FunctionCodeBlockDemo[19364:18889544] 格式正确
    2020-09-27 11:27:20.754961+0800 FunctionCodeBlockDemo[19374:18890477] 待校验的字符串为:11010119900
    2020-09-27 11:27:20.755146+0800 FunctionCodeBlockDemo[19374:18890477] 格式错误
    

    车牌号格式校验(粤A8888澳)

    /** 车牌号格式校验(粤A8888澳) */
    + (BOOL)checkCarID:(NSString *)carID;
    
    // 车牌号格式校验(粤A8888澳)
    + (BOOL)checkCarID:(NSString *)carID
    {
        if (carID.length != 7)
        {
            return NO;
        }
        
        NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-hj-zA-HJ-Z]{1}[a-hj-zA-HJ-Z_0-9]{4}[a-hj-zA-HJ-Z_0-9_\u4e00-\u9fa5]$";
        NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
        return [carTest evaluateWithObject:carID];
    }
    

    输出结果为:

    2020-09-27 11:28:45.787246+0800 FunctionCodeBlockDemo[19396:18892383] 待校验的字符串为:粤A8888澳
    2020-09-27 11:28:45.788894+0800 FunctionCodeBlockDemo[19396:18892383] 格式正确
    2020-09-27 11:29:00.628475+0800 FunctionCodeBlockDemo[19412:18893523] 待校验的字符串为:粤A88
    2020-09-27 11:29:00.628629+0800 FunctionCodeBlockDemo[19412:18893523] 格式错误
    

    银行卡格式验证

    /** 银行卡格式验证 */
    + (BOOL)isBankCard:(NSString *)bankCard;
    
    // 银行卡格式验证
    + (BOOL)isBankCard:(NSString *)bankCard
    {
        if (bankCard.length < 16)
        {
            return NO;
        }
        
        NSInteger oddSum = 0;// 奇数求和
        NSInteger evenSum = 0;// 偶数求和
        NSInteger allSum = 0;
        NSInteger cardNumberLength = (NSInteger)[bankCard length];
        
        // 取了最后一位数
        NSInteger lastNumber = [[bankCard substringFromIndex:cardNumberLength - 1] intValue];
        
        // 测试的是除了最后一位数外的其他数字
        bankCard = [bankCard substringToIndex:cardNumberLength - 1];
        
        for (NSInteger i = cardNumberLength - 1 ; i >= 1;i--)
        {
            NSString *tempString = [bankCard substringWithRange:NSMakeRange(i - 1, 1)];
            NSInteger tempValue = [tempString integerValue];
            if (cardNumberLength % 2 == 1 )// 卡号位数为奇数
            {
                if((i % 2) == 0)// 偶数位置
                {
                    tempValue *= 2;
                    if (tempValue >= 10)
                    {
                        tempValue -= 9;
                    }
                    evenSum += tempValue;
                }
                else// 奇数位置
                {
                    oddSum += tempValue;
                }
            }
            else
            {
                if((i % 2) == 1)
                {
                    tempValue *= 2;
                    if(tempValue >= 10)
                    {
                        tempValue -= 9;
                    }
                        
                    evenSum += tempValue;
                }
                else
                {
                    oddSum += tempValue;
                }
            }
        }
        
        allSum = oddSum + evenSum;
        allSum += lastNumber;
        if((allSum % 10) == 0)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    

    输出结果为:

    2020-09-27 11:30:28.815633+0800 FunctionCodeBlockDemo[19439:18895693] 待校验的字符串为:6221882600114166800
    2020-09-27 11:30:28.815815+0800 FunctionCodeBlockDemo[19439:18895693] 格式正确
    2020-09-27 11:30:55.528978+0800 FunctionCodeBlockDemo[19456:18896846] 待校验的字符串为:6221882
    2020-09-27 11:30:55.529138+0800 FunctionCodeBlockDemo[19456:18896846] 格式错误
    

    二、混合校验

    6~16位数字/字母/下划线组成的密码格式校验

    /** 6~16位数字/字母/下划线组成的密码格式校验 */
    + (BOOL)checkPassword:(NSString *)passwordString;
    
    // 6~16位数字/字母/下划线组成的密码格式校验
    + (BOOL)checkPassword:(NSString *)passwordString
    {
        NSString *pattern = @"^[A-Za-z0-9_]{6,16}$";
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
        return [predicate evaluateWithObject:passwordString];
    }
    

    输出结果为:

    2020-09-27 11:33:42.387144+0800 FunctionCodeBlockDemo[19490:18899693] 待校验的字符串为:Ab123456
    2020-09-27 11:33:42.388725+0800 FunctionCodeBlockDemo[19490:18899693] 格式正确
    2020-09-27 11:34:56.640539+0800 FunctionCodeBlockDemo[19547:18903956] 待校验的字符串为:AAAAAAAAA
    2020-09-27 11:34:56.642130+0800 FunctionCodeBlockDemo[19547:18903956] 格式正确
    2020-09-27 11:34:33.141514+0800 FunctionCodeBlockDemo[19529:18902842] 待校验的字符串为:12345678910
    2020-09-27 11:34:33.143212+0800 FunctionCodeBlockDemo[19529:18902842] 格式正确
    2020-09-27 11:35:08.997045+0800 FunctionCodeBlockDemo[19557:18904861] 待校验的字符串为:A
    2020-09-27 11:35:08.998633+0800 FunctionCodeBlockDemo[19557:18904861] 格式错误
    

    判断字符串是否全为[(数字)、(字母)、(数字|字母)、(汉字)]

    /** 判断字符串是否全为[(数字)、(字母)、(数字|字母)、(汉字)] */
    + (BOOL)isAllNumber:(NSString *)string;
    
    // 判断字符串是否全为[(数字)、(字母)、(数字|字母)、(汉字)]
    +(BOOL)isAllNumber:(NSString *)string
    {
        NSString *condition = @"^[0-9]*$";// 是否都是数字
        //NSString *condition = @"^[A-Za-z]+$";//是否都是字母
        //NSString *condition = @"^[A-Za-z0-9]+$";//是否都是字母和数字{6,16}
        //NSString *condition = @"^[A-Za-z0-9]{6,16}$";//是否都是字母和数字且长度在[6,16]
        //NSString *condition = @"^[\u4e00-\u9fa5]{0,}$";//只能输入汉字
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",condition];
        return [predicate evaluateWithObject:string];
    }
    

    输出结果为:

    2020-09-27 11:36:12.740745+0800 FunctionCodeBlockDemo[19578:18906474] 待校验的字符串为:12345
    2020-09-27 11:36:12.742450+0800 FunctionCodeBlockDemo[19578:18906474] 格式正确
    2020-09-27 11:36:29.644180+0800 FunctionCodeBlockDemo[19589:18907523] 待校验的字符串为:123AB
    2020-09-27 11:36:29.645862+0800 FunctionCodeBlockDemo[19589:18907523] 格式错误
    

    邮箱格式验证

    /** 邮箱格式验证 */
    + (BOOL)isEmail:(NSString *)email;
    
    // 邮箱格式验证
    +(BOOL)isEmail:(NSString *)email
    {
        NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
        NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
        return [emailTest evaluateWithObject:email];
    }
    

    输出结果为:

    2020-09-27 11:37:41.488681+0800 FunctionCodeBlockDemo[19608:18909237] 待校验的字符串为:212226@gmail.com
    2020-09-27 11:37:41.490287+0800 FunctionCodeBlockDemo[19608:18909237] 格式正确
    2020-09-27 11:37:53.592850+0800 FunctionCodeBlockDemo[19621:18910897] 待校验的字符串为:212226@gmail
    2020-09-27 11:37:53.594444+0800 FunctionCodeBlockDemo[19621:18910897] 格式错误
    

    判断密码格式是否正确

    /** 判断密码格式是否正确 */
    + (BOOL)validatePassword;
    
    // 判断密码格式是否正确
    + (BOOL)validatePassword:(NSString *)passwordString
    {
        NSString *length = @"^\\w{6,20}$";// 长度
        NSString *number = @"^\\w*\\d+\\w*$";// 数字
        NSString *lower = @"^\\w*[a-z]+\\w*$";// 小写字母
        NSString *upper = @"^\\w*[A-Z]+\\w*$";// 大写字母
        
        NSPredicate *lengthPredicate = [NSPredicate predicateWithFormat: @"SELF MATCHES %@", length];
        NSPredicate *numberPredicate = [NSPredicate predicateWithFormat: @"SELF MATCHES %@", number];
        NSPredicate *lowerPredicate = [NSPredicate predicateWithFormat: @"SELF MATCHES %@", lower];
        NSPredicate *upperPredicate = [NSPredicate predicateWithFormat: @"SELF MATCHES %@", upper];
        
        BOOL lengthFlag = [lengthPredicate evaluateWithObject:passwordString];
        BOOL numberFlag = [numberPredicate evaluateWithObject:passwordString];
        BOOL lowerFlag = [lowerPredicate evaluateWithObject:passwordString];
        BOOL upperFlag = [upperPredicate evaluateWithObject:passwordString];
        return (lengthFlag && numberFlag && lowerFlag && upperFlag);
    }
    

    输出结果为:

    2020-09-27 11:42:43.940427+0800 FunctionCodeBlockDemo[19707:18915828] 待校验的字符串为:212226gmail
    2020-09-27 11:42:43.942167+0800 FunctionCodeBlockDemo[19707:18915828] 格式错误
    2020-09-27 11:43:18.438507+0800 FunctionCodeBlockDemo[19727:18917110] 待校验的字符串为:212226Gmail
    2020-09-27 11:43:18.440343+0800 FunctionCodeBlockDemo[19727:18917110] 格式正确
    

    三、文字校验

    昵称

    /** 昵称 */
    + (BOOL)validateNickname:(NSString *)nickname;
    
    // 昵称
    + (BOOL)validateNickname:(NSString *)nickname
    {
        NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{1,8}$";
        NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
        return [passWordPredicate evaluateWithObject:nickname];
    }
    

    输出格式为:

    2020-09-27 11:44:25.958665+0800 FunctionCodeBlockDemo[19752:18918805] 待校验的字符串为:212226Gmail
    2020-09-27 11:44:25.960309+0800 FunctionCodeBlockDemo[19752:18918805] 格式错误
    2020-09-27 11:44:41.052712+0800 FunctionCodeBlockDemo[19762:18919744] 待校验的字符串为:谢佳培
    2020-09-27 11:44:41.054403+0800 FunctionCodeBlockDemo[19762:18919744] 格式正确
    

    四、文本框校验

    输入判断小数点后两位

    #pragma mark - UITextFieldDelegate
    
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        BOOL judgeTwoDecimalPlaces = [self JudgeTwoDecimalPlaces:textField shouldChangeCharactersInRange:range replacementString:string];
        return judgeTwoDecimalPlaces;
        return YES;
    }
    
    #pragma mark - 校验方法
    
    // 输入判断小数点后两位
    - (BOOL)JudgeTwoDecimalPlaces:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        // 如果输入的是“.”  判断之前已经有"."或者字符串为空
         if ([string isEqualToString:@"."] && ([textField.text rangeOfString:@"."].location != NSNotFound || [textField.text isEqualToString:@""]))
         {
             NSLog(@"文本框内容为:%@,已经有小数点了,禁止再输入小数点",textField.text);
             return NO;
         }
        
         // 拼出输入完成的str,判断str的长度大于等于“.”的位置+4,则返回false,此次插入string失败
         // "379132.424",长度10,"."的位置6, 10>=6+4
         NSMutableString *str = [[NSMutableString alloc] initWithString:textField.text];
         [str insertString:string atIndex:range.location];
        
         if (str.length >= [str rangeOfString:@"."].location + 4)
         {
             NSLog(@"文本框内容为:%@,小数点后两位以上了,禁止输入",str);
             return NO;
         }
        
         return YES;
    }
    

    输出结果为:

    2020-09-28 09:54:21.094432+0800 FunctionCodeBlockDemo[26113:19329511] 文本框内容为:1234.555,小数点后两位以上了,禁止输入
    2020-09-28 09:54:40.504445+0800 FunctionCodeBlockDemo[26113:19329511] 文本框内容为:1234.5,已经有小数点了,禁止再输入小数点
    

    Demo

    Demo在我的Github上,欢迎下载。
    FunctionCodeBlock

    参考文献

    相关文章

      网友评论

          本文标题:IOS基础:常用的格式校验方法

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