Only numbers input

Godot Version

4.3

Question

How do I make Line edit accept only numbers?

Just set the virtual keyboard type from “Default” to “Number” in LineEdit.

1 Like

That I didn’t know, just made the change in my project to suit.

Thanks

Ryn

1 Like

It didn’t work. The line edit still doesn’t accept only numbers.

Then? It accepts letters?

If so, then can you show a screenshot that you have done it properly?

This seems to work

var old_text := ""

func _ready() -> void:
    line_edit.text_changed.connect(_something)

func _something(text: String) -> void:
    if text.is_empty() or text.is_valid_int():
        old_text = text
    else:
        line_edit.text = old_text
1 Like

If you want just numbers why not use a SpinBox node instead?

Doesn’t work for me either, spin box mode is not an option, not if you want to enter a large number… Seems to only work on mobile platforms with virtual keyboards.

Ryn

That only sets the virtual keyboard that will appear on mobile devices, don’t avoid typing letters with a normal keyboard


Can you elaborate what doesn’t work with SpinBox? Because you can put any number Godot handles in a spinbox, also this is not a mode, is a different node from LineEdit

1 Like

Sorry, I mean you can still type letters into a spin box… I always validate the input as an INT anyway so I avoid errors.

Would be nice to lock out anything but numbers, but I have a work around.

Ryn

But the invalid characters will be removed when you press enter or change the focus, so no signal is emitted and the invalid value will not be returned

1 Like

I am not sure why there is no built-in feature for that, but you can give a try to this:

var final_text = ""

func _ready()
   line_edit.text_changed.connect(on_line_edit_text_changed)

func on_line_edit_text_changed(text):
   if text.is_valid_int():
      final_text = text
   
   line_edit.text = final_text

But it would still accept some symbols like “+”, “-”, etc.If you want, I can fix it.

1 Like

True, but still would be nice to mask out the letters like so many other dev environments. :slight_smile:

Ryn

Thanks @KingGD

I already do exactly this, as well as moving the caret column to the end… Been my practice to always validate any input, even when mask abilities are available

My method removes the - & + characters also by storing the result as an int not text, so I just str() the int back if an invalid character is entered.

Ryn

1 Like

@KingGD

I’d like to ask why you connect signals that way instead of using the Node Tab, which means you don’t have to clutter up your _ready() method and it also creates the signal func for you?

Custom signals sure…

Ryn

oh, thanks! works perfectly.

I’m making a math game, so I want the input to be exclusive to numbers only.

oh, thanks! it works too, but there was just one “else” missing at the end.

1 Like
extends LineEdit

# Signal to notify when invalid input is attempted
signal invalid_input_attempted

func _ready():
    # Connect to the text_changed signal
    text_changed.connect(_on_text_changed)

func _on_text_changed(new_text: String):
    # If empty, allow it
    if new_text.is_empty():
        return
        
    # Check if the new text is a valid number
    if !new_text.is_valid_float():
        # Revert to previous valid text
        var cursor_pos = get_caret_column()
        text = text
        set_caret_column(cursor_pos - 1)
        invalid_input_attempted.emit()

# Optional: Allow only integers by adding this function
func _on_text_submitted(new_text: String):
    # Convert to integer if needed
    if new_text.is_valid_float():
        text = str(int(float(new_text)))

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.