Text Fields

Hello,


How do I get a text field to not allow a 0 before the first integer?


Swift 4


Thank you

Replies

Typically, this sort of thing is done by using Cocoa bindings to bind the value of a text field to a model or controller property (the normal scenario), and using KVC validation to check the value:


https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/Validation.html


Note that, by default, text fields validate their contents only when Enter or Tab is pressed, or the text field tries to lose first-responder status. If you want to check as individual characters are entered, make sure you set the "continuous validation" option for the binding.

The way I would do it is to connect an IBAction to the editindChanged event of the field.


    @IBAction func testChange(_ sender: UITextField) {

}

Where I would check and discard if first character typed is 0

I tried that but how do I validate the value?

I tried that but how do I validate the value?

Are you asking how to tell whether a string begins with zero? If so, that’s very easy:

let s: String = …
let hasLeadingZero = s.hasPrefix("0")

However, things are never that simple when dealing with user input. For example:

  • You may want to accept or fail with leading whitespace

  • You may want to accept or fail with leading sign characters (“+” and “-”)

  • You may want to deal with localised digits; for example, an Arabic user would type U+0660 ARABIC-INDIC DIGIT ZERO not U+0030 DIGIT ZERO

I can explain how to handle all of these case but you need to be specific about your requirements.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

As Quinn said, you know what is a valid input ; so your code must check for this.


In addition, you need to decide what do do when input is not valid: discard the typed character ? discard the whole entry ?


And, extremely important, take care that user can understand what's happening there. It is a very bad user experience to see an input refused without undertsnading precisely the reason why.

It is a very bad user experience to see an input refused without undertsnading precisely the reason why.

Agreed. My preferred option for this is to allow the user to type any input and then have a separate label that that dynamically updates to explain why the current input is unacceptable.

<rant>

I wish all password fields worked this way. It’s so frustrating to enter a password and then, when you try to save, be told that certain characters are not allowed, or that the two copies of your password don’t match.

</rant>

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I find the idea of the explanation label, updated in sync with textField editing pretty ggod ; could apply to more than passwords in fact.

An option would be to programmatically remove leading 0s after the user submits the input values.