Im unsure how to stop inputs or replace inputs with nothing inside a LineEdit. is their a way to do this.
Also how do i detect for spaces or whitespace?.
Please help me 🙂

  • Here's an example:

    var username_regex: RegEx
    
    func _ready() -> void:
    	# Compile RegEx pattern for parsing username input.
    	username_regex = RegEx.new()
    	var regex_err: Error = username_regex.compile("\\s")
    	assert(regex_err == OK)
    
    func some_other_function(username_input: String) -> void:
    	# Check if username input contains whitespace.
    	var regex_match: RegExMatch = username_regex.search(username_input)
    	if regex_match != null:
    		print_debug("username contains whitespace")
    	else:
    		print_debug("username does not contain whitespace")

    Ask if you need an explanation for the "\\s".

DaveTheCoder
By using
func _on_username_input_text_changed(new_text):
if new_text == "":

i want to add a boundary that stops the user from inputting a space while creating their username.
I will also want to be able to check for a couple of things like special characters and word count.

Their also might be another way of doing this if so how? thx : )

I'm assuming that this script is attached to the LineEdit, so that "text" is the LineEdit's text property.

This is untested, but should work (Godot 4.3):

func _on_username_input_text_changed(new_text: String) -> void:
    # Remove non-printable characters (tabs, line feeds, etc.) and spaces.
    text = new_text.strip_escapes().replace(" ", "")

Reference: https://docs.godotengine.org/en/4.3/classes/class_string.html#methods

This could also be done with a RegEx. That's my preference (because regular expressions are cool, and using them makes you feel superior to people who can't use them), but it's tricky if you're not familiar with RegEx.

    DaveTheCoder
    Thx for the suggestion but , i think the way im checking the input will make it to complicated when looking for special characters / text length .So i will try to create a method using RegEx.

    DaveTheCoder
    After getting stuck i cam up with this

    and it says "null instanace" when i run the code. to the text_changed.connect(_on_username_input_text_changed) line why is that?

      Place ~~~ on lines before and after the code in your post.

      Also, explain what you want the code to do.

        DaveTheCoder
        Sorry idk how to make all code stuff :/
        I am using a LineEdit to make a person write a username (email & password later) then add boundary's like a word limit and a unexpected like special characters when typing the username.
        This code i have i think, takes an input from the user converts it into a var(current1) then checks from the compile list (RegEx) each word input then spits it back out with the usernameEntry piece of code i think.
        🙂

          TheRedPandaKing Sorry idk how to make all code stuff :/

          To post code here:
          A. Type ~~~ on a line by itself, before the code.
          B. Copy/paste the code from the Godot editor.
          C. Type ~~~ on a line by itself, after the code.

          TheRedPandaKing add boundary's like a word limit and a unexpected like special characters when typing the username

          1. By "word limit" do you mean character limit? Do you want to limit the length of the username that the user can type? You can do that by setting the LineEdit's max_length property.

          2. What do you want to do if the user types special characters? Discard them? Display an error message to the user?

          TheRedPandaKing it says "null instanace" when i run the code. to the text_changed.connect(_on_username_input_text_changed) line why is that?

          1. I can't see your whole project, and I can't copy/paste from a screenshot. My guess, since you're using % instead of $, is that the usernameEntry node is not a child of the node to which that script is attached, and it's not "ready" yet, so you can't reference it in that script's _ready().

            DaveTheCoder

            
            extends Control
            
            @onready var usernameEntry: LineEdit = %usernameEntry 
            
            func _ready():
            	usernameEntry.text_changed.connect(_on_username_input_text_changed) 
            	usernameEntry.grab_focus() 
            	usernameEntry.text = ""
            	
            	
            func _process(delta):
            	pass
            
            func _on_create_button_pressed():
            	print("create pressed")
            
            
            
            func _on_username_input_text_changed(new_text):
            	var current1:String = ""
            	
            	var regex1: RegEx = RegEx.new()
            	regex1.compile("[A-Za-z0-9]")
            	
            	for valid_character in regex1.search_all(new_text): 
            		current1 += valid_character.get_string()
            	usernameEntry.set_text(current1)
            		
            	print(new_text)

            Idk it wont copy pate correctly, this is the best i can do :/

              You need three (3) tilde's ~~~, not two ~~.

              This is your last chance. One more mistake and I'll transfer you to xyz.

              DaveTheCoder

              The max length piece of code at the end is so when they type over the limit of 5, i want all new inputs to be discarded expect if they press backspace. and if they were to press a special character i want it to be discarded and also send a signal, so i can make a pop up visible saying incorrect input--. further on after i understand it how to do this i want to add in more unexpected inputs to then cancel them ie they press create with nothing inside the username Another pop next to the username will say "you need a text to create a login".

              i used This as a source for the code

              If the script you posted is attached to the node register_screen, then this is how to reference its LineEdit child node UsernameInput:
              @onready var usernameEntry: LineEdit = $UsernameInput

              Try that, and add a print statement in _on_username_input_text_changed to verify that it's called when the user types in the LineEdit.

                DaveTheCoder
                Thank you holy, it works great!
                how would i make (label.visible = true) when it detects an input thats not apart of the RegEx compile list. would i need to add another for loop that checks their inputs?
                it would be great 🙂

                According to the documentation, that signal would be connected the same way as the text_changed signal.

                  DaveTheCoder
                  What do you mean im not sure i understand, i also don't now how to connect a signal to the for loop that's adding in the valid characters.
                  And also how would i turn the current1 into a variable outside of the filed so that i can check if it's empty or is their a better way?

                    TheRedPandaKing how to connect a signal to the for loop that's adding in the valid characters

                    Why are you using a loop?

                    You need to explain clearly and completely what you want to do. Pretend that you've hired a programmer to write the code that handles the UsernameInput, and you're giving him the specification for how it will behave from the user's perspective. The programmer will use the specification to write the code without any further communication with you, so it must be complete.

                    Ill do my best

                    extends Control
                    
                    
                    
                    @onready var ULT = $UsernameInput/UsernameLengthTag
                    #@onready 
                    @onready var UNIT = $UsernameInput/UsernameNoInputTag
                    
                    @onready var usernameEntry: LineEdit = $UsernameInput #sets whatever is in the Username lineedit into a var
                    
                    const i: int = 1
                    var username: String = ""
                    
                    
                    func _ready():
                    	usernameEntry.text_changed.connect(_on_username_input_text_changed) # i think it connects the current lineedit text to whats being changed below
                    	usernameEntry.grab_focus() 
                    	usernameEntry.text = ""
                    
                    func _process(delta):
                    	pass
                    
                    func _on_create_button_pressed():
                    	
                    	print("create pressed")
                    	print(username)
                    
                    
                    
                    func _on_username_input_text_changed(new_text):
                    	
                    	var old_caret_position: int = usernameEntry.caret_column
                    	
                    	
                    	
                    	
                    	
                    	var regex1: RegEx = RegEx.new() #this is the spesfici list of compiled RegEx letters
                    	regex1.compile("[A-Za-z0-9]")
                    	var current1:String = ""
                    	var diff: int = regex1.search_all(new_text).size() - new_text.length()
                    	
                    	for valid_character in regex1.search_all(new_text): #searchs the inputs that are being taken from a list in (regex1.compile)
                    		current1 += valid_character.get_string() #adds what ever is being input after being checked into the (current1 string)
                    			
                    #add a way to add a signal-that is played when you input somthing not in the compile list
                    	usernameEntry.set_text(current1) #set's what the (current1) is to the usernameEntry which is being displayed
                    	usernameEntry.caret_column = old_caret_position 	+ diff
                    	
                    	print(current1)
                    	
                    	if current1.length() >= 12:
                    		ULT.visible = true
                    	elif current1.length() <= 12:
                    		ULT.visible = false	

                    I want to have a user be able to create a Username that saves to a file on their computer. The username must not allow space's, special characters and cant be longer then 12. If the user inputs a special characters/space, goes over the word limit, or inputs nothing. I want to display a pop up that tells them they can't do that respective thing.

                    I also want to be able to do this with a Email input where it checks if you have a @ and a .com at the end. But also a password that wants you to input at least 1 number.

                    How i do this im not sure, im very new to godot and coding in general.

                      TheRedPandaKing cant be longer then 12

                      1. That can be done easily by setting max_length to 12. Then the user cannot enter more than 12 characters. That's handled automatically by the LineEdit.

                      TheRedPandaKing must not allow space's, special characters

                      1. Instead of looping through text, after the user presses the submission button, you can use a single RegEx check, or a String method call, to determine whether the user's input contains any of those characters. If it does, you can display a message that explains that the username must not contain those characters. Then the user would have to make the corrections himself and resubmit.

                      2. Another choice is to silently remove spaces and special characters as the user types them. That can be done with a single RegEx replacement, or with the String method call I posted above. After the user presses the submission button, you could optionally display a message that spaces and/or special characters were removed; but that's probably not needed, since the user can see the username that's displayed.

                      In any case, there could be a static note near the input field that explains the restrictions.

                      The email and password could be handled in a similar way.

                      Does the above seem reasonable?