how to make a restriction on the characters entered on LineEdit?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By WellStacked

(LineEdit use for nickname)
Interested in restrictions on the number and only Latin

p.s. sorry for my English: GoogleTranslate Edition

What do you want to know? It’s impossible to help you with this amount of information.

fpicoral | 2019-02-05 21:54

That the player could not enter more than 10 characters and that it was only Latin letters (use lineedit)

WellStacked | 2019-02-06 00:28

More precisely. It is necessary to count the number of сharacters entered and check whether the text entered contains only Latin characters.

WellStacked | 2019-02-06 00:37

:bust_in_silhouette: Reply From: fpicoral

With latin characters you mean ã, ñ, õ, é, etc, right?
And you want to block the limit of characters to 10 or you can’t type more than 10?

If you want to block up to 10 characters and the latin letters that you mentioned are the ones above, go to the node inspector and set the Max Length to 10.

To check if the text contains a latin letter, what you could do were link the LineEdit signal text_changed to its script and use the find() method to see if the text contains the letters that you want to check.

To be more pratical, I would suggest using a for loop, like this:

extends LineEdit

var latin_characters = ["ã", "ñ", "ó"] #Add all the characters that you want to look for

func _on_LineEdit_text_changed(new_text):
	for letter in latin_characters:
		if new_text.find(letter) > 0: #If it's not present, return -1
			print("Latin character %s found!"%letter)

If you want to make sure it’s ONLY latin characters, you could create another array with all the other characters and do the reverse logic, to make sure there are only the desired characters.

extends LineEdit

var latin_characters = ["ã", "ñ", "ó"] #Add all the characters that you want to look for
var normal_char = ["a", "b", "c"]

func _on_LineEdit_text_changed(new_text):
	var only_latin = true
	
	for normal in normal_char:
		if new_text.find(normal) >= 0:
			only_latin = false
			
	if only_latin:
		for letter in latin_characters:
			if new_text.find(letter) >= 0: #If it's not present, return -1
				print("Latin character %s found!"%letter)
	else:
		print("Only latin letters please!")

fpicoral | 2019-02-06 01:05

And to count the ammount of latin letters, the code would be this one, where amount is the amount of latin characters:

extends LineEdit

var latin_characters = ["ã", "ñ", "ó"] #Add all the characters that you want to look for
var normal_char = ["a", "b", "c"]

var amount = 0
var text_array = []

func _on_LineEdit_text_changed(new_text):
	var only_latin = true
	
	count_latin_letters()
	
	for normal in normal_char:
		if new_text.find(normal) >= 0:
			only_latin = false
			
	if only_latin:
		for letter in latin_characters:
			if new_text.find(letter) >= 0: #If it's not present, return -1
				print("Latin character %s found!"%letter)
	else:
		print("Only latin letters please!")

func count_latin_letters():
	text_array = []
	amount = 0
	
	for i in text:
		text_array.append(i)
		
	for letter in text_array:
		if letter in latin_characters:
			amount += 1

fpicoral | 2019-02-06 01:12

Your code helped me a lot, thanks! <3
p.s. I meant just “normal_char” (no latin), just wrote wrong

WellStacked | 2019-02-06 01:27

Glad I could help!
If you need help with something else, please ask!

fpicoral | 2019-02-06 01:59