ios – UITextField Range input from min value to max value of 5 -> 30


I have a text field and I am trying to add error handling for a condition were the min value is 5 and the max is 30. I can’t seem to figure out a solution or condition to satisfy this as if I input the value 1 then it will return false since that’s lower then 5. That causes an issue though as I can never do something like “14”.

Not sure if I should also add a set of numeric values to validate if the number inputed is less than a range from 0 > 5. Any help or direction would be appreciated!

This is the code I currently have:

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

    if let value = Int(valueString), let maxValue, (value >= Int(truncating: maxValue)), let minValue, value <= Int(truncating: minValue) {
        if value == 0 {
            textField.text = ""
        } else {
            textField.text = currencyFormatter.string(from: NSNumber(value: value))
        }
    }

    return true
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img