I want UMG text box to only accept float numbers, but can’t figure out how can that be done.
Hello,
You maybe could use a spin box instead of a text box ?
I assume the spinny part can’t be disabled?
I don’t see a way to disable it but I have just give a try and here is a quick work around to solve this : Do a function in graph : From spinbox : get value / set min slider value and set max slider value". Use function on event construct and on value changed. Spin seems not working.
A not so great workaround would be to add an event on text changed (of the text box) then do a check on the text if it contains any letters, symbols (etc.) and if so then remove all the letters and replace the text of the text box (using Set Text), it should be almost instant so it should be good enough(?)
I have created my own version of a Spin Box … Text field that is read only … placed in a horizontal box with a button in front of the field called - and one behind called +
Click - … take current value of field and subtract one … Click + … take current value of field and add one.
On some of my fields I have a max number and min number … if the user goes past that it just flicks back to the min and max depending on direction.
I have used a similar approach for fields that require almost a drop down feature … it allows me to control the values and I don’t have to deal with pesky little Drop Down Lists that are so small you can hardly read the text.
Text box, on update convert text to string, check is numeric, break/if false then set to 1 (or some number) and print an error message/if true then set variable. You could probably leave out the error message and people would probably get the memo if you kept changing the text back to a number and the text box content was implied to be a number.
That was my easy and efficient work around. Sorry for the late response, i just stumbled across this searching about this same issue.
I think the cleanest way to validate is to do it OnTextChanged - this keeps your input box from getting crowded before commit.
Here’s how I did it:
// Define this lambda in [YourWidget]::Construct()
auto fnOnTextChanged = =](const FText& Val)
{
if (!Val.IsNumeric()){ // Returns false if the text is not completely numeric
auto chopped = FText::FromString(Val.ToString().LeftChop(1)); // This gets rid of the last character in the text
TextPtr->SetText(chopped); // Set the text (you have to cache/SAssignNew the SEditableTextBox for this to work)
}
};
// Bind the lambda to your widget
// ChildSlot]...
SAssignNew(TextPtr, SEditableTextBox)
.OnValueChanged_Lambda(fnOnTextChanged)
On text change is a very “web” way to do stuff.
Instead, create a widget in which you add a text box, edit the code. Override the OnKeyDown function.
Drag off from the input, type is key to get the inital key information.
Drag off from that and check that it is numeric.
When it is numeric handle the input how you see fit (normally call a function that writes the key to the string by appending) and return Handled.
When the key is not numeric return Unhandled in the function.
This works without fail, all the time. And also absolutely prevents the input from being changed to something that isn’t valid before the determination to check if it is valid or invalid occurs.
Also, nice Necro…
Fails for me as the text box still captures the text with handled or unhandled being used, I used OnKeyUp and force set the text if its set to anything other than numbers this give people some feedback that its only supports numbers.
God bless you