4

我需要限制用户只能输入小数点后两位数。我通过在文本字段委托 shouldChangeCharactersInRange 中的代码实现了这一点。但它允许输入多个点。如何限制这个?提前致谢。

            NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
            NSArray *sep = [newString componentsSeparatedByString:@"."];
            if([sep count]>=2)
            {
                NSString *sepStr=[NSString stringWithFormat:@"%@",[sep objectAtIndex:1]];
                NSLog(@"sepStr:%@",sepStr);

                return !([sepStr length]>2);
            }
            return YES;
4

6 回答 6

12

最好的方法是Regular Expression在这样的shouldChangeCharactersInRange:委托方法中使用

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    NSString *newStr = [textField.text stringByReplacingCharactersInRange:range withString:string];

    NSString *expression = @"^([0-9]*)(\\.([0-9]+)?)?$";

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:nil];
    NSUInteger noOfMatches = [regex numberOfMatchesInString:newStr
                                                        options:0
                                                          range:NSMakeRange(0, [newStr length])];
    if (noOfMatches==0){
        return NO;
    }
    return YES;
}

实施此有效字符串后为:
12.004546
4546.5456465
.5464
0.454

很快....

您还可以使用此限制小数点后的整数个数Regular Expression

@"^([0-9]*)(\\.([0-9]{0,2})?)?$"

实施此有效字符串后为:
12.00
4546.54
.54
0.45

于 2014-02-06T12:43:24.570 回答
3
 -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

NSString *sepStr;

NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

NSArray *sep = [newString componentsSeparatedByString:@"."];
if([sep count]>=2)
   {
        sepStr=[NSString stringWithFormat:@"%@",[sep objectAtIndex:1]];
        NSLog(@"sepStr:%@",sepStr);
        if([sepStr length] >2)
       {
        return NO;
       }
    else 
       {
        return YES;
       }

   }
   return YES;
 }
于 2012-10-16T09:23:51.247 回答
2

当输入一个点时,你应该检查一个点是否已经存在,如果存在则返回 NO。

 NSString * newString = [textField.text stringByReplacingCharactersInRange: range withString: string];
 NSArray * sep = [newString componentsSeparatedByString: @"."];
 if([string isEqualToString:@"."] && [sep count] > 1){
    //already a . is  there.. so don't allow new one
    return NO;
 }
 if ([sep count] >= 2) {
     NSString * sepStr = [NSString stringWithFormat: @"%@", [sep objectAtIndex: 1]];
     NSLog(@"sepStr:%@", sepStr);

     return !([sepStr length] > 2);
 }
 return YES;
于 2012-10-16T09:16:57.110 回答
1

使用 reg ex 更新了 Swift 3 的答案"^([0-9]*)(\\.([0-9]+)?)?$"

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

    let newText = ((textField.text ?? "") as NSString).replacingCharacters(in: range, with: string)

    let expression = "^([0-9]*)(\\.([0-9]+)?)?$"

    guard let regex = try? NSRegularExpression(pattern: expression, options: .caseInsensitive) else {
        return false
    }
    let noOfMatches = regex.numberOfMatches(in: newText, options: [], range: NSMakeRange(0, newText.characters.count))

    return noOfMatches > 0
}
于 2017-02-20T15:42:35.753 回答
0

您可以使用此方法:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
     NSRange temprange = [textField.text rangeOfString:@"."];
        if ((temprange.location != NSNotFound) && [string isEqualToString:@"."])
        {
            return NO;
        }
    return YES;
}
于 2013-02-08T05:09:11.057 回答
0
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSArray *sep = [newString componentsSeparatedByString:@"."];
    if([string isEqualToString:@"."]){
        if([textField.text containsString:@"."]){
            return NO;
        }
    }
    if([sep count] >= 2)
    {
        NSString *sepStr=[NSString stringWithFormat:@"%@",[sep objectAtIndex:1]];
        return !([sepStr length]>2);
    }
    return YES;
}
于 2018-01-25T11:20:53.043 回答