如何限制UITextField中的小数点数?

时间:2012-05-01 20:43:32

标签: ios uitextfield decimal-point

我有一个UITextField,当点击时会在左下方显示一个带小数点的数字键盘。我试图限制字段,以便用户只能放置1个小数点

e.g。
2.5 OK
2..5不行

16 个答案:

答案 0 :(得分:42)

像这样实现shouldChangeCharactersInRange方法:

// Only allow one decimal point
// Example assumes ARC - Implement proper memory management if not using.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSArray  *arrayOfString = [newString componentsSeparatedByString:@"."];

    if ([arrayOfString count] > 2 ) 
        return NO;

    return YES;
}

这会创建一个由小数点分割的字符串数组,因此如果有多个小数点,我们将在数组中至少包含3个元素。

答案 1 :(得分:14)

以下是带正则表达式的示例,示例仅限制一个小数点和2位小数。您可以调整它以满足您的需求。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSString *expression = @"^[0-9]*((\\.|,)[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;
}

答案 2 :(得分:6)

对于Swift 2.3来防止用户在两个地方后输入十进制数 -

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool
{
    let decimalPlacesLimit = 2
    let rangeDot = txtPrice.text!.rangeOfString(".", options: .CaseInsensitiveSearch)

    if rangeDot?.count > 0
    {
        if (string == ".")
        {
            print("textField already contains a separator")
            return false
        }
        else {

            var explodedString = txtPrice.text!.componentsSeparatedByString(".")
            let decimalPart = explodedString[1]
            if decimalPart.characters.count >= decimalPlacesLimit && !(string == "")
            {
                print("textField already contains \(decimalPlacesLimit) decimal places")
                return false
            }
        }
    }
}

答案 3 :(得分:5)

在接受的答案的基础上,以下方法验证了三种在处理货币格式时有用的案例:

  1. 非常大量
  2. 小数点后超过2个字符
  3. 超过1小数点
  4. 确保正确设置了文本字段的委托,您的类符合UITextField协议,并添加以下委托方法。

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
      // Check for deletion of the $ sign
      if (range.location == 0 && [textField.text hasPrefix:@"$"])
        return NO;
    
      NSString *updatedText = [textField.text stringByReplacingCharactersInRange:range withString:string];
      NSArray *stringsArray = [updatedText componentsSeparatedByString:@"."];
    
      // Check for an absurdly large amount
      if (stringsArray.count > 0)
      {
        NSString *dollarAmount = stringsArray[0];
        if (dollarAmount.length > 6)
          return NO;
      }
    
      // Check for more than 2 chars after the decimal point
      if (stringsArray.count > 1)
      {
        NSString *centAmount = stringsArray[1];
        if (centAmount.length > 2)
          return NO;
      }
    
      // Check for a second decimal point
      if (stringsArray.count > 2)
        return NO;
    
      return YES;
    }
    

答案 4 :(得分:4)

Swift 3 实现此UITextFieldDelegate方法以防止用户输入无效的数字:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let text = (textField.text ?? "") as NSString
    let newText = text.replacingCharacters(in: range, with: string)
    if let regex = try? NSRegularExpression(pattern: "^[0-9]*((\\.|,)[0-9]*)?$", options: .caseInsensitive) {
        return regex.numberOfMatches(in: newText, options: .reportProgress, range: NSRange(location: 0, length: (newText as NSString).length)) > 0
    }
    return false
}

它使用逗号或点作为小数分隔符。您还可以使用此模式限制小数位数:"^[0-9]*((\\.|,)[0-9]{0,2})?$"(在本例中为2)。

答案 5 :(得分:2)

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
    if(textField == min_textfield )
    {
        if([textField.text rangeOfString:@"."].location == NSNotFound)
        {
            if([string isEqualToString:@"."] )
            {
                flag_for_text = 1;
            }
            else 
            {
                textField.text = [NSMutableString stringWithFormat:@"%@",textField.text];
            }
        }
        else 
        {
            if([string isEqualToString:@"."])
            {
                return NO;
            }
            else 
            {
                textField.text = [NSMutableString stringWithFormat:@"%@",textField.text];
            }
        }
    }
}

答案 6 :(得分:2)

试试这个: -

public func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

    if(text == "," || text == "." ){
        let countdots = textView.text!.componentsSeparatedByString(".").count - 1

        if countdots > 0 && (text == "." || text == "," )
        {
            return false
        }
    }

    return true
}

答案 7 :(得分:1)

Swift 3

无需创建数组并检查计数。限制用户只能放置1个十进制标记。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if (textField.text?.contains("."))! && string.contains(".")
    {
        return false
    }
    else
    {
        return true
    }
}

答案 8 :(得分:1)

雨燕4

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

    // Allow to remove character (Backspace)
    if string == "" {
        return true
    }

   // Block multiple dot
    if (textField.text?.contains("."))! && string == "." {
        return false
    }

    // Check here decimal places
    if (textField.text?.contains("."))! {
        let limitDecimalPlace = 2
        let decimalPlace = textField.text?.components(separatedBy: ".").last
        if (decimalPlace?.count)! < limitDecimalPlace {
            return true
        }
        else {
            return false
        }
    }
    return true
}

Objective-C

//Create this variable in .h file or .m file
float _numberOfDecimal;

//assign value in viewDidLoad method
numberOfDecimal = 2;

#pragma mark - TextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    // Allow to remove character (Backspace)
    if ([string isEqualToString:@""]) {
        return true;
    }

    // Block multiple dot
    if ([textField.text containsString:@"."] && [string isEqualToString:@"."]) {
        return false;
    }

    // Check here decimal places
    if ([textField.text containsString:@"."]) {
        NSString *strDecimalPlace = [[textField.text componentsSeparatedByString:@"."] lastObject];

        if (strDecimalPlace.length < _numberOfDecimal) {
            return true;
        }
        else {
            return false;
        }
    }
    return true;
}

答案 9 :(得分:0)

在您设置UITextField委托的任何对象中,添加一个回答"[- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string]"的方法。

然后您可以使用NSNumberFormatter对象,也可以强制检查已存在的小数位标记(如果已存在小数点,则返回NO。)

答案 10 :(得分:0)

简短告诉,数字格式如下[NSString stringWithFormat:@"%9.5f", x];其中5是“,”之后的小数。

答案 11 :(得分:0)

我制定了解决方案,可以控制小数位数,因此用户只能键入一个小数点分隔符,并且还可以控制小数位数。

只需正确设置 decimalPlacesLimit 值。

见方法:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"text on the way: %@", string);
    NSUInteger decimalPlacesLimit = 2;

    NSRange rangeDot = [textField.text rangeOfString:@"." options:NSCaseInsensitiveSearch];
    NSRange rangeComma = [textField.text rangeOfString:@"," options:NSCaseInsensitiveSearch];
    if (rangeDot.length > 0 || rangeComma.length > 0){
        if([string isEqualToString:@"."]) {
            NSLog(@"textField already contains a separator");
            return NO;
        } else {
            NSArray *explodedString = [textField.text componentsSeparatedByString:@"."];
            NSString *decimalPart = explodedString[1];
            if (decimalPart.length >= decimalPlacesLimit && ![string isEqualToString:@""]) {
                NSLog(@"textField already contains %d decimal places", decimalPlacesLimit);
                return NO;
            }
        }
    }

    return YES;
}

答案 12 :(得分:0)

Swift 4

在UITextField中避免多个小数点(。或,)的有效而简单的方法:

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if(string == "," || string == "." ){

        if ((textField.text?.contains(","))! || (textField.text?.contains("."))!){
            return false
        }
    }
    return true
}

答案 13 :(得分:0)

雨燕4

最大整数个数为4,即9999,最大十进制数限制为2。因此,最大个数可以为9999.99

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


    // 100 is the tag value of our textfield
    /*or you may use "if textfield == myTextField{" if you have an IBOutlet to that textfield */
    if textField.tag == 100 {

        //max length limit of text is 8
        if textField.text!.count > 8 && string != "" {
            return false
        }

        let maxLength = 8
        let currentString: NSString = textField.text! as NSString 

//使用以下代码如果您要向该文本字段输入价格,并希望在用户开始在该文本字段中键入内容时在开始时自动插入$,或者您可以在开始处放置其他字符而不是$。否则,注释以下3行if条件代码

        if currentString.length == 0 {
            priceTextField.text = "$"
        }

//插入新输入的字符后的新字符串

        let newString: NSString =
            currentString.replacingCharacters(in: range, with: string) as NSString


        if newString.length > maxLength{
            return false
        }

        if (textField.text!.range(of: ".") != nil) {
            let numStr = newString.components(separatedBy: ".")
            if numStr.count>1{
                let decStr = numStr[1]
                if decStr.length > 2{
                    return false
                }
            }
        }

        var priceStr: String = newString as String

        if (textField.text!.range(of: "$") != nil) {
            priceStr = priceStr.replacingOccurrences(of: "$", with: "")
        }

        let price: Double = Double(priceStr) ?? 0

        if price > 9999.99{
            return false
        }

        switch string {
        case "0","1","2","3","4","5","6","7","8","9":
            return true
        case ".":
            let array = Array(textField.text!)
            var decimalCount = 0
            for character in array {
                if character == "." {
                    decimalCount = decimalCount + 1
                }
            }

            if decimalCount == 1 {
                return false
            } else {
                return true
            }
        default:

            let array = Array(string)
            if array.count == 0 {
                return true
            }
            return false
        }
    }
    return true
}

答案 14 :(得分:0)

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if([string isEqualToString:@"."]) {
        BOOL containsDecimal = [textField.text containsString:@"."];
        return !containsDecimal;
    }
    return YES;
}

如果文本字段文本已经包含“。”然后返回“否”,否则返回“是”。

答案 15 :(得分:0)

SWIFT 5

改进

信息:不允许:

  • 开头的分隔符
  • 零开始时加上另一个数字,除非您在之后添加分隔符

1:将键盘类型设置为:小数键盘

enter image description here

2:复制过去

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    //!\ set the keyboard type to : Decimal Pad /!\\
    // CUSTOM SETUP
    let c = NSLocale.current.decimalSeparator ?? "."
    let limitBeforeSeparator = 2
    let limitAfterSeparator = 2
    // ---------
    
    
    var validatorUserInput:Bool = false
    
    let text = (textField.text ?? "") as NSString
    let newText = text.replacingCharacters(in: range, with: string)
    
    
    // Validator
    let pattern = "(?!0[0-9])\\d*(?!\\\(c))^[0-9]{0,\(limitBeforeSeparator)}((\\\(c))[0-9]{0,\(limitAfterSeparator)})?$"
    if let regex = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) {
        validatorUserInput = regex.numberOfMatches(in: newText, options: .reportProgress, range: NSRange(location: 0, length: (newText as NSString).length)) > 0
    }
     
    
    if validatorUserInput {
        // setting data or something eles before the return
        if let char = string.cString(using: String.Encoding.utf8) {
            let isBackSpace = strcmp(char, "\\b")
            if (isBackSpace == -92 && textField.text?.count == 1) {
                print("Backspace was pressed")
                print(newText)
                // do something...
                
            } else {
                print("Number Added")
                print(newText)
                // do something...
                
            }
        }
        return validatorUserInput
    } else {
        return validatorUserInput
    }
}

3:在方法中进行设置,如果您想要x分隔符前后的最大位数

 let limitBeforeSeparator = 2 
 let limitAfterSeparator = 2