How to Make a Gui Textbox only Allow numbers and 1 Decimal

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to make a Textbox That Only Allows Numbers and Only 1 Decimal

  1. What is the issue? Include screenshots / videos if possible!
    I can’t find a way to do it. And all my attempts were unsuccessful

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I checked and could not find a solution

I want to make this so players would not scale the GUI to a Scale that Completely hides the GUI.

My Current Code:

local sizebox = script.Parent.sizebox.Text

script.Parent.sizebox:GetPropertyChangedSignal("Text"):Connect(function()
	if script.Parent.sizebox.Text:find("%.") > 1 then
		script.Parent.sizetest.Visible = false
	elseif script.Parent.sizebox.Text:find("%.") <= 1 then
		script.Parent.sizetest.Visible = true
	end
end)

As you can see my skills are not that great.

2 Likes

a very quick simple way to do it is by using tonumber().

if tonumber(text) ~= nil then
    -- text is as number
else
    -- text is not a number
end

I hope this is what you mean by only numbers and one decimal, the Text Box should only allow integer numbers with one decimal place after it, ie. 13.5, 3.3, and not 3.33, or 1.52. I think I put the necessary variables where they needed to go for you. Hopefully, this puts you in a good enough spot to customize the rest how you need to. :slight_smile:

-- Get the Textbox object
local textBox = script.Parent.sizebox

-- Function to validate user input
local function validateInput(input)
	-- Check if the input is empty
	if input == "" then
		return false
	end

	-- Check if the input contains non-numeric or non-decimal characters
	if string.match(input, "[^%d%.]") then
		return false
	end

	-- Check if the input contains more than one decimal point
	if string.match(input, "%.") then
		local decimalIndex = string.find(input, "%.")
		if decimalIndex == #input or decimalIndex == nil or string.sub(input, decimalIndex+2):match("%d") then
			return false
		end
	end
	return true
end

-- Function to handle user input
local function handleInput()
	local input = textBox.Text
	if not validateInput(input) then
		textBox.Text = "" --Custom text for invalid entry 
		--Executes when there is not valid input
		script.Parent.sizetest.Visible = false
	else
		--Executes when there is valid input
		script.Parent.sizetest.Visible = true
	end
end

-- Connect the handleInput function to the TextBox object's events
textBox.Focused:Connect(function()
	-- Clear the Textbox when the user focuses on it
	textBox.Text = ""
end)

textBox.FocusLost:Connect(function()
	handleInput()
end)

2 Likes

Wow Thanks. This works perfectly! :+1:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.