Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to check what type of text user typed?

Asked by
NorteX_tv 101
5 years ago

I have a TextBox that needs to be a number.

How can I check if TextBox text type is only numbers? (maybe something like: if string.type(path) == "number"

3 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
function check(var)
    if tonumber(var) ~= "" and tonumber(var) ~= nil then
        return true
    end
end

check(23)

try this

0
btw this will still return true if there is text and numbers in the text box ForgotenR4 68 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Try this:

local textbox = script.Parent.TextBox

textbox.Changed:Connect(function()
local text = tonumber(textbox.Text)
    if text ~= nil then
        print(text)
    end
end)

If the text has any letter added, the if-then statement will prevent whatever you're trying to do from being fired.

Log in to vote
0
Answered by 5 years ago

To not let them type non-digit characters, you can use the string pattern %D+. It matches 1 or more occurrences of non digit characters.

With that in mind...

textbox.Text = textbox.Text:gsub("%D+", "")

will look for non digit characters and replace them with empty string "".

textbox:GetPropertyChangedSignal("Text"):Connect(function()
    textbox.Text = textbox.Text:gsub("%D+", "")
end)

Answer this question