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

问题描述 投票:1回答:5

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

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

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

我怎样才能实现它?

ios uitextfield uitextfielddelegate
5个回答
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
投票

使用此正则表达式代码:

- (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(第二个粗体)表示小数点后面的字符。

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


0
投票

这是使用Swift和Regular Expressions实现它的方法:

  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 }

0
投票

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

编辑

虽然文本字段具有例如462.&user将退格结果触及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;
}

-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;

} 
© www.soinside.com 2019 - 2024. All rights reserved.