How would I create a number only TextBox?

How would I go about making a number only textbox? To elaborate I want a textbox that you can only type numbers in.

11 Likes
TextBox.FocusLost:Connect(function(Return)
    if not (Return) then return end
    if (TextBox.Text:match("^%d+$")) then
        --//What the code does
    end
end)

If TextBox.Text:match("^%d+$") confuses you, my apologies. I am simply exercising my string manipulation as I saw this as a good opportunity to do so. For a short explanation, the function is comparing a “pattern” to the string :match() is being called on. %d+ or “digits” you could say, is what I look for. By using the two anchor points ^ and $ , I say that I want to compare digits from the start to the end of the string.

If that is still difficult, you can simplify the conditional with:

if (tonumber(TextBox.Text)) then
    --//Code
end

or you can always use typeof() in some cases

3 Likes

I’d use TextBox:GetPropertyChangedSignal("Text") for this, something like this

TextBox:GetPropertyChangedSignal("Text"):Connect(function()
	TextBox.Text = TextBox.Text:gsub('%D+', '');
)

That way, you can ensure the textbox only contains arabic numerals (0-9). If you want to include decimals, you can to :gsub('[^%d{INSERT DECIMAL SEPARATOR HERE}]', ''), replace {Insert decimal separator here} with a decimal symbol.

If it’s (but unlikely) locale-aware (different numbering systesm like eastern arabic numerals), then I can’t help you for that, sorry.

46 Likes

you can use tonumber(TextBox.Text) i would use it for the future purposes

what is the Return Parameter used for?

In that case it’s the Property that changed in the TextBox. If you were to use that script I would suggest using :GetPropertyChangedSignal("Text") just like @Blockzez said earlier.
If you look at my example return detects when the player click enter or in some way submits their entry in the typing area.
https://developer.roblox.com/en-us/api-reference/event/TextBox/FocusLost
https://developer.roblox.com/en-us/api-reference/event/Instance/Changed

im very sorry for such a late reply to your topic but is there any chance you can help me, IDK how to make a minimum number for my textbox, for example, anything typed below 5 is ignored.

Get the TextBox character length and just test if its above the requirement. if it is then use Index and remove it

hey thanks for replying but I solved I my problem im currently busy so I will edit and show others the solution when I get the chance

End that with a “end)” or it won’t work, otherwise, it works perfectly :slight_smile: