Skip to main content
Frédéric C.
Inspiring
June 20, 2018
Answered

Filtering textfield input on iOS

  • June 20, 2018
  • 1 reply
  • 665 views

Hi,

I need to ask people to enter a 6-digit number (an integer) in a textfield, with only characters ranging from 0 to 9.

I use a regular TextField, with a DECIMAL type soft keyboard. On the CHANGE event, I check the contents of the TextField, and if I detect a non-digit character, I remove it from the TextField.text string, then re-assign the cleaned-up string to the TextField. On Windows, it works perfectly fine.

But on iOS, I get a weird behavior. If I type a comma (which is present on the decimal soft keyboard on iOS), my code tries to clean the TextField string, and when I assign it back to the TextField, the comma is still there, and the text of the TextField is fully selected (as if I had made a long press on the text, and selected it all). So there is a double issue: the cleaned-up string is not reassigned properly to the TextField content, and all the content of the TextField is selected, so the next key press on the soft keyboard will erase everything.

Do you know if there is some way to filter the contents of a TextField on iOS while you are typing, to limit the range of characters you can enter?

Thank you very much!

This topic has been closed for replies.
Correct answer natural_criticB837

Hey,

you should be able to use the .restrict field on the TextField to limit the input characters to the characters you set. E.g.

tf.restrict = "0123456789";

should work in your case.

1 reply

natural_criticB837
natural_criticB837Correct answer
Legend
June 20, 2018

Hey,

you should be able to use the .restrict field on the TextField to limit the input characters to the characters you set. E.g.

tf.restrict = "0123456789";

should work in your case.

Frédéric C.
Inspiring
June 20, 2018

Oh, awesome! Thank you so much for your quick reply. I'm always amazed at the power of AIR. It indeed works, and now I can't type a comma, so this is great.

However, I also have to limit the number to 6 digits max. In my code, I simply check the TextField.text length, and if it is greater then 6, I only reassign a substring to the TextField. However, as explained above, reassigning a value to a TextField causes this strange behavior of selecting the whole text instead. I don't think there is a native TextField property that limits the numbers of characters, right?

natural_criticB837
Legend
June 20, 2018

Well, how hard would it be to check that It's .maxChars