+1 vote

how can i do this with this code

var normal_char = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0"]

func _on_LineEdit_text_entered(new_text):
    if new_text != "":if !new_text in FS.level_names:
        level_name = new_text
        level_path = "res://saves/scene_saves/"+str(level_name)
        $Label.text = level_name
        $Label.visible = true
        can_play = true
        $LineEdit.queue_free()
        FS.level_names += [level_name]
        FS.save_data(level_name+".txt",FS.level_names)

when i use

if !new_text.find(normal_char) <=0:

it dont work

in Engine by (93 points)

try
print(normalchar)
and
print(new
text)

it prints:

!@#!@#

[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

print(newtext.find(normalchar))

-1
but when i use

print(newtext.find("a"))

it prints correctly

1 Answer

+1 vote
Best answer

Try a for loop

for _char in new_text:
    if _char not in normal_char:
        return #exit the _on_LineEdit_text_entered func

where the return is in the code you can replace with a function to notify of invalid entry for example

$LineEdit.clear()
$LineEdit.set_placeholder("Please enter valid text")

Strings also have a is_valid_identifier func

if new_text.is_valid_identifier:
    if !new_text.has("_"):
        return
by (4,982 points)
selected by

Thank you!!!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.