UITextField只接受一个小数点,小数点前最多3位数&小数点后最多2位数

时间:2016-04-07 13:20:38

标签: ios uitextfield uitextfielddelegate

我想限制UITextField只接受一个小数点。

小数点前后最多允许3位数&小数点后最多允许2位数。

请注意,最小数字可以是1,小数不能输入第一个。

我怎样才能实现它?

5 个答案:

答案 0 :(得分:1)

您可以在同一场景中使用以下代码。

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)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;
    }

NSUInteger newLength = [textField.text length] + [string length] - range.length;
    if(range.length + range.location > textField.text.length)
    {
        return NO;
    }
    if ([newStr containsString:@"."])
    {
        return newLength <= 6;
    }
    return newLength <= 3;

    //  return YES;
}

由于.被视为一个角色。总共将是6个字符。您可以在条件中调整值。

答案 1 :(得分:0)

这是使用 Swift 正则表达式实现它的方法:

  1. 设置文本字段的委托。这可以通过代码或IB来完成。

  2. 要验证用户输入的文本,您可以将代码添加到您的代理中,类似于以下内容:

    // Number of digits that are allowed before decimal point
    let kMaximumIntegerDigits = 3
    // Number of digits that are allowed after decimal point
    let kMaximumFractionDigits = 2
    
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    
    // The replacement string is empty when the user have removed some characters.
    if string.isEmpty == true { return true }
    
    // Get the current string the text field holds.
    let currentText = textField.text ?? ""
    
    // Check the limits.
    if (string.characters.count + currentText.characters.count) > 6 { return false }
    
    // Get the string we are expecting to obtain after the replacement.
    var resultingText = currentText
    resultingText.insertContentsOf(string.characters, at: currentText.startIndex.advancedBy(range.location))
    
    // Check the final string with the help of the regular expression.
    let regex = "^(?:(?:[0-9]{1,\(kMaximumIntegerDigits)}[.][0-9]{0,\(kMaximumFractionDigits)})|(?:[0-9]){1,\(kMaximumIntegerDigits)})$"
    let regexRange = resultingText.rangeOfString(regex, options: .RegularExpressionSearch)
    if regexRange == nil { return false }
    
    return true
    
    }
    
  3. 最后,您应该在用户尝试结束编辑会话时验证生成的文本。你可以这样做:

    func textFieldShouldEndEditing(textField: UITextField) -> Bool {
     // Get the current input string.
     guard let currentText = textField.text else { return false }
    
     // Create the regular expression for the final check.
     let regex = "^(?:(?:[0-9]{1,\(kMaximumIntegerDigits)}[.][0-9]{1,\(kMaximumFractionDigits)})|(?:[0-9]){1,\(kMaximumIntegerDigits)})$"
     let regexRange = currentText.rangeOfString(regex, options: .RegularExpressionSearch)
     if regexRange == nil { 
     // Show an alert to the user with the message that explains what the input is expected...
     return false 
     }
    
     // Make additional clean-up and finalize the editing session.
     return true
    }
    

答案 2 :(得分:0)

非常感谢所有人的帮助。通过引用这些答案,我在下面给出了答案。

修改

虽然文本字段具有例如462.&amp;用户将退格结果触及462,理想情况下应该导致46

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        NSString *strTextField = [textField.text stringByReplacingCharactersInRange:range withString:string];
    // restrict textfield from entering ./0 at first place
    if (strTextField.length > 0) {
        NSString *theCharacterAtIndex0 = [NSString stringWithFormat:@"%c", [strTextField characterAtIndex:0]];
        if ([theCharacterAtIndex0 isEqualToString:@"."] || [theCharacterAtIndex0 isEqualToString:@"0"]) {
            return NO;
        }
    }
    //    NSLog(@"%@ , %@", textField.text, strTextField);

    // automatically add decimal point after 3 digits entered
    if (![textField.text containsString:@"."]) {
        if (strTextField.length == MAX_LENGTH_BeforeDecimal && ![strTextField containsString:@"."]) {
            strTextField = [strTextField stringByAppendingString:@"."];
            textField.text = strTextField;
            return NO;
        }
    }

    // when decimal is deleted
    if ([textField.text containsString:@"."]) {
        if (![strTextField containsString:@"."]) {
            int indeOfdecimal = (int)[textField.text rangeOfString:@"."].location;
            NSString *strBeforeDecimal = [textField.text substringToIndex:indeOfdecimal];
            textField.text = strBeforeDecimal;
        }
    }

    NSArray *separator = [strTextField componentsSeparatedByString:@"."];
    // to restrict textfield to single decimal
    if([separator count] > 2 ) {
        return NO;
    }
    if([separator count] >= 2) {
        // restrict the max digits before & after decimal points
        NSString *sepStr0 = [NSString stringWithFormat:@"%@",[separator objectAtIndex:0]];
        NSString *sepStr1 = [NSString stringWithFormat:@"%@",[separator objectAtIndex:1]];
        if ([sepStr0 length] > 3) {
            return NO;
        }
        if ([sepStr1 length] > 2) {
            return NO;
        }
    }
    return YES;
}

答案 3 :(得分:0)

使用此正则表达式代码:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
   // Only allow one decimal point and 2 digits after it
   NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
   NSString *expression = @"^[0-9]{0,3}$*((\\.|,)[0-9]{0,2})?$";
   NSError *error = nil;
   NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionCaseInsensitive error:&error];
   NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString options:0 range:NSMakeRange(0, [newString length])];
   return numberOfMatches != 0;
   return YES;
 }

此处使用的正则表达式为“ ^ [0-9] {0, 3 } $ *((\。|,)[0-9] {0, 2 })?$“。

数字3(第一个粗体)表示小数点前的字符,数字2(第二个粗体)表示小数点后的字符。

您可以使用此正则表达式并根据需要创建规则。

答案 4 :(得分:-1)

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

// iPad Validations for Number only

NSCharacterSet *characterSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];

NSString *filteredString = [[string componentsSeparatedByCharactersInSet:characterSet] componentsJoinedByString:@""];

if (![string isEqualToString:filteredString]) {
    return NO;
}
  // HERE WRITE CODE FOR VALIDATION TO ALLOW CHARACTER AFTER "." 
 return YES;

}