Skip to content

Instantly share code, notes, and snippets.

@t-karcher
Created May 5, 2020 05:32
Show Gist options
  • Save t-karcher/d656aa567eef117e2750bd1b6b9f5505 to your computer and use it in GitHub Desktop.
Save t-karcher/d656aa567eef117e2750bd1b6b9f5505 to your computer and use it in GitHub Desktop.
Filters out characters from input in LineEdit using a RegExp pattern of allowed characters
extends LineEdit
var allowed_characters = "[A-Za-z]"
func _on_LineEdit_text_changed(new_text):
var old_caret_position = self.caret_position
var word = ''
var regex = RegEx.new()
regex.compile(allowed_characters)
for valid_character in regex.search_all(new_text):
word += valid_character.get_string()
self.set_text(word)
caret_position = old_caret_position
@t-karcher
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment