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
You should do the following inside the above method:
I created a class with the help method that can be call from any place in your project.
Swift code:
Then you can use in your uiviewcontroller, in textfield delegate method with any textfield validations
In its most basic form you can do this:
However, you also need to check if
newText
is an integer, becauseintValue
returns 0 when the text starts with other characters.