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.

6 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.

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.

10 Likes

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

1 Like

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

You can also use regular expressions

271032-regex.png

6 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):

5 Likes

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

For number only pattern is : [0-9]

Try this, its work with me

You can just literally bypass this by setting the default values for the variables

spin box may resolve the problem. It works for me .

Since There is no such things that unreal has provided just to take numbers as input here is an easy workaround in blueprints that you can use

  1. Create a Onchanged event on your input box.
  2. Convert the output text to string and check whether it is a numeric value or not(you can do so by a node called is numeric from kismet string library).
  3. Create a simple branch and in true just set the text of input box and in false leave it empty.

It’s custum convertor (Text to only num text)

For anyone who comes by this, the is numeric node includes characters like +, -, *, ., etc. You can’t rely on using only that node when you check if a string has only numbers.

As for why you don’t use the spin box widget: There is no control on the max number of characters a player can type before committing. A player can enter 9999999999999 into a spin box as long as they don’t commit. You also can’t set the text like you can with an editable text box, only set value, so you can not monitor spin box text entry with the OnValueChanged event. Finally, spin boxes also have the issue with +,-,* characters.

While Regex works, it’s a bit complicated to expose it to blueprints without a plugin. Regex isnt available to blueprint by default (as far as I know).

My solution is below. I know it can be further improved, but it can help get you started.

The way it works is:

First, check if the length of the text is <=0 or >3. This locks the size. Set 3 to whatever character limit you want. If string is outside the size limits, set the textbox to the last good CurrentText. (Do nothing). Otherwise continue.

Then check if the last entered character (Right count 1) is a digit (0-9). If true, set Current Text. (Allow change). Otherwise set the textbox to the last good CurrentText. (Do nothing).

This solution allows you to lock a textbox to whatever length and characters you might want, not just numerical digits. Change that list of digits to whatever you please. You can change it to a range if you want it a bit cleaner.

Something to note: If a player starts text entry in the middle or start of text, they can enter non-numeric characters. This is because Right Count 1 is only checking the right most char. To make this full proof, you have to iterate over the full string array and check every char, not just Right.