TextField positive integer format / filter


#1

What would the best approach be to ensure that a user can only enter a positive integer into a TextField?


#2

The most obvious solution to me was to change the keyboard type via the keyboardType property and set it to number however, this solution is far from perfect due to the fact that the user can still enter negative, decimal or even negative decimal numbers.


#3

Most apps just restrict with keyboard type then finally clean the input value and validate against whatever character limitation they want to keep.

A JavaScript way of doing it could be, cleaning the input value on each text change event - make sure you ignore the text change event fired when you set the text value programatically.

If you prefer a native way, use try input filters (Android) and delegates (iOS). The below link has sample code implements same features for max length validation. You may modify it for character restrictions.