Copy link to clipboard
Copied
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!
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Well, how hard would it be to check that It's .maxChars
Copy link to clipboard
Copied
Okay, now I feel ashamed lol. My only poor excuse is that I'm not on a computer right now, and checking the AS3 docs were not very easy for me. Thank you so much for holding my hand on that one!
Cheers!