-4

What code would I use to have the user type in a number between 18 and 120 for their age?

Something like:

agetextfield must be = to a number between 18-120

If they type in a string that is not a number, alert the user "Please enter Age as a numerical value e.g 19".

If they type in a number less than 18, alert the user " User must be over the age of 18"

If they type in a number greater than 120, alert the user " User Must be between the ages of 18 and 120.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
benpalmer661
  • 49
  • 10

1 Answers1

-1

You can use this for entry type, it will show a numeric keyboard only.

textField.keyboardType = .numberPad

And then set the delegate textField.delegate = self

so you can use this:

func textFieldDidEndEditing(_ textField: UITextField) {
    if Int(textField.text!)! < 18 || Int(textField.text!)! > 120 {
        // Alert
    }
}
Farid Al Haddad
  • 833
  • 9
  • 19