In Swift, is it possible to set a max INT value to a UITextField?
My use-case is I have 5 text fields that need to have a maximum int value. These values range from
Here's an update for Swift 4 and later that ensure the value entered is an integer in the range 0 to 5000.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newText = (textField.text! as NSString).replacingCharacters(in: range, with: string) as String
if let num = Int(newText), num >= 0 && num <= 5000 {
return true
} else {
return false
}
}