UITextField : restrict the maximum allowed value (

2020-02-29 11:34发布

I have a UITextField, I'd like to restrict the maximum allowed input value in the field to be 1000. That's when user is inputting number inside, once the input value is larger than 999, the value in input field won't be updated anymore unless user is inputting value less than 1000.

I think I should use UITextField delegate to limit the input:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
  //How to do
}

But I am not sure how to implement it. Any suggestions?

==========Update=============

my input field not only allow user to input integer, but also float value like 999,03

5条回答
beautiful°
2楼-- · 2020-02-29 11:55

You should do the following inside the above method:

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

//first, check if the new string is numeric only. If not, return NO;
NSCharacterSet *characterSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789,."] invertedSet];
if ([newString rangeOfCharacterFromSet:characterSet].location != NSNotFound)
{
    return NO;
}

return [newString doubleValue] < 1000;
查看更多
够拽才男人
3楼-- · 2020-02-29 11:55
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField.tag == 3)
    {
        if(textField.text.length >3 && range.length == 0)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
}
查看更多
叛逆
4楼-- · 2020-02-29 12:04

I created a class with the help method that can be call from any place in your project.

Swift code:

class TextFieldUtil: NSObject {

    //Here I am using integer as max value, but can change as you need
    class func validateMaxValue(textField: UITextField, maxValue: Int, range: NSRange, replacementString string: String) -> Bool {

        let newString = (textField.text! as NSString).stringByReplacingCharactersInRange(range, withString: string)

        //if delete all characteres from textfield
        if(newString.isEmpty) {
            return true
        }

        //check if the string is a valid number
        let numberValue = Int(newString)

        if(numberValue == nil) {
            return false
        }

        return numberValue <= maxValue
    }
}

Then you can use in your uiviewcontroller, in textfield delegate method with any textfield validations

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

    if(textField == self.ageTextField) {
        return TextFieldUtil.validateMaxValue(textField, maxValue: 100, range: range, replacementString: string)
    }
    else if(textField == self.anyOtherTextField) {
        return TextFieldUtils.validateMaxValue(textField, maxValue: 1200, range: range, replacementString: string)
    }
    return true
}
查看更多
狗以群分
5楼-- · 2020-02-29 12:09

In its most basic form you can do this:

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

    newText = [textField.text stringByReplacingCharactersInRange:range withString:string];

    return [newText intValue] < 1000;
}

However, you also need to check if newText is an integer, because intValue returns 0 when the text starts with other characters.

查看更多
我只想做你的唯一
6楼-- · 2020-02-29 12:19
if([string length])
{
  if (textField == txt)
  {
      NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
      return !([newString length] > 1000);
  }                     
}
查看更多
登录 后发表回答