美文网首页笔记篇
UITextField 判断 输入输入框中 小数的处理

UITextField 判断 输入输入框中 小数的处理

作者: 失忆的程序员 | 来源:发表于2021-09-02 17:10 被阅读0次
#pragma mark ----- 抽出来 作为公共的方法 小数点 判断 用于 输入 1.2元 逻辑
+ (BOOL)x_JudgmentinputDecimal:(NSString *)textFieldText cont:(NSString *)string
{
    NSString *str = [NSString stringWithFormat:@"%@%@", textFieldText, string];
    //匹配以0开头的数字
    NSPredicate *predicate0 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^[0][0-9]+$"];
    //匹配两位小数、整数
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^(([1-9]{1}[0-9]*|[0])\.?[0-9]{0,2})$"];
    BOOL sucess = ![predicate0 evaluateWithObject:str] && [predicate1 evaluateWithObject:str] ? YES : NO;
    return sucess;
}

在这里用 ,这样用

UITextField 的 UITextFieldDelegate
tf.delegate = self;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.textF)
    { // 金额输入判断
        NSString *str = [NSString stringWithFormat:@"%@%@", textField.text, string];
        //匹配以0开头的数字
        NSPredicate *predicate0 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^[0][0-9]+$"];
        //匹配两位小数、整数
        NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", @"^(([1-9]{1}[0-9]*|[0])\.?[0-9]{0,2})$"];
        BOOL sucess = ![predicate0 evaluateWithObject:str] && [predicate1 evaluateWithObject:str] ? YES : NO;
        return sucess;
    }
    return YES;
}

相关文章

网友评论

    本文标题:UITextField 判断 输入输入框中 小数的处理

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