Oaks-view
Oaks-view

Reputation: 433

Restrict kivy's TextInput to only ascii characters

Please is there a way i can make a TextInput to disallow non ascii characters. So that when text is entered into the TextInput if a non-ascii character is typed it will not be added to TextInput. Just like the way one uses int filter, thus only whole numbers will be allowed in the TextInput
Please an example code would very be helpful. Thanks in advance

Upvotes: 2

Views: 1810

Answers (2)

bmbigbang
bmbigbang

Reputation: 1408

one potential solution is to use .decode() with the errors='ignore' flag on a string (eg of textinput). for example:

"food ресторан".decode("ascii", errors='ignore')

will replace all the chars it can to ascii silently

EDIT** updated example with przyczajony's suggestion to use filters:

class AsciiInput(TextInput):

    def insert_text(self, string, from_undo=False):
        string = string.decode("ascii", errors='ignore')
        return super(AsciiInput, self).insert_text(string, from_undo=from_undo)

Upvotes: 3

jligeza
jligeza

Reputation: 4703

TextInput filtering is described in docs, even with examples: Filter

Use regular expressions to check whenever the entered string contains wanted characters ([A-Za-z0-9 ]). If it passes, then return the string.

Upvotes: 4

Related Questions