How to set TextBox Input to numbers only?

Well im trying to make a TextBox so i can get numbers from the player but if the player use letters my code fail so im asking if there is anyway to limit the player input type at TextBoxes.
Thank you.

5 Likes

UX-wise, having an active textbox that doesn’t respond to certain keys could be confusing. You would normally let them type whatever, then use OnTextCommitted to check if the string is valid and give them an error message if necessary.

If you do have to make it happen, since this is non-standard behavior you’ll likely have to override the TextBox’s input events. If you don’t want to get into the code, you might be able to hack it by placing another widget on top of the text box (an image or something) and giving that widget input events for only the number keys. When it’s focused and a number key is hit, pass that number to the text box. When enter is hit, clear the text box and proceed.

mate what a genius move haha :stuck_out_tongue: wil try just for fun

1 Like

There is nothing out of the box from EPIC AFAIK for what you want. What you can do is the following.

  • Create an OnChange Event for your textbox.
  • Convert the text input from the event to string and check ‘IsNumeric’ on string.
  • If ‘True’ then store the value of the textbox into a text variable named ‘LastEnteredNumber
  • If ‘False’ then set the value of the textbox as the variable ‘LastEnteredNumber

I don’t have UE4 with me now. That’s why the text based solution. Hopefully this works for you.

Cheers.

6 Likes

Hi,
This is what I’ve done in blueprints to set input to number only:

You can also use regular expressions

271032-regex.png

3 Likes

Generally good idea, but won’t work properly cause user can past a string with any length while event gonna be called only one time. also symbols “.-+” are accepted by IsNumeric, here is better version

2 Likes

don’t work in ue 4.18, first character is as number, but other character get too letter, please help

1 Like

Lazy solution:
Use a SpinBox and disable the slider (You also have to disable it in the “Style” tab if you want it to disappear completely):

2 Likes

Uses Regex. More about it: https://regex101.com/

For number only pattern is : [0-9]

Try this, its work with me