如何验证文本字段中只允许单个小数点的文本字段?

时间:2012-10-16 09:05:08

标签: iphone ios validation uitextfield

我需要限制用户在小数点后只输入两位数。我通过在textfield delegate 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;

6 个答案:

答案 0 :(得分: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
0.5464
0.454

所以.......

您还可以使用此Regular Expression来确定小数点后的整数倍数。@"^([0-9]*)(\\.([0-9]{0,2})?)?$"

实现此有效字符串后:
12.00
4546.54
0.54
0.45

答案 1 :(得分: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;
 }

答案 2 :(得分: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;

答案 3 :(得分:1)

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

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
}

答案 4 :(得分: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;
}

答案 5 :(得分: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;
}