Making textbox accept minus only at the 1st character of the string

I have a textbox that accepts only integers, decimals, and the minus sign, but the minus can be placed anywhere and I don’t want that.

Current code:

textBox:GetPropertyChangedSignal("Text"):Connect(function()
	textBox.Text = string.gsub(textBox.Text, "[^%d.%-]", "")
end)

I searched through the dev forum and found these topics:

I made the current code with the help of them but noticed that the minus can be placed anywhere on the textbox, even a the end.

If you use any number-only textbox at roblox studio (from the properties tab or any other) you’ll get what I mean, the minus sign can only be placed at the 1st character position.

Any help is appreciated.

Edit: found this page on Lua strings patterns and character classes but I can’t find out how to use them for what I want to achieve, but leaving it here might be useful for someone I guess.

1 Like

Not sure how to make the code better (so to get rid of the elseif yknow) but this is the best solution I could find

textBox:GetPropertyChangedSignal("Text"):Connect(function()
	textBox.Text = string.gsub(textBox.Text, "[^%d.%-]", "")
	if typeof(tonumber(textBox.Text)) == "number" then
		
	elseif string.len(textBox.Text) == 1 and string.find(textBox.Text, "-", 1) then
		
	else
		textBox.Text = string.sub(textBox.Text, 1, string.len(textBox.Text) - 1)
	end
end)
1 Like
textBox:GetPropertyChangedSignal("Text"):Connect(function()
	textBox.Text = string.match(v.Text, "%-?%d+%.?%d*")
end)
1 Like

I changed it to:

textBox:GetPropertyChangedSignal("Text"):Connect(function()
	if textBox.Text ~= nil then
		textBox.Text = string.match(textBox.Text, "%-?%d+%.?%d*")
	end
end)

but throws this:

Unable to assign property Text. string expected, got nil

in the output when I delete a character with backspace (the original you sent also throws it)

1 Like

this is because string match returns nil instead of “” when nothing matches. (forgot about that)
try this version instead

textBox:GetPropertyChangedSignal("Text"):Connect(function()
	textBox.Text = string.match(v.Text, "%-?%d*%.?%d*") or ""
end)
1 Like

Now it’s working but has a lot of bugs:

I can’t type a minus if there’s nothing in the text box

If there’s a minus and a number and I press backspace at the end of the text it deletes both

If I type a minus at the left of a number and that number is not the first then it deletes this number

And probably more, I’m trying something with

textBox.Text:sub(2)

But not having too much success

So far I got this:

v:GetPropertyChangedSignal("Text"):Connect(function()
	v.Text = string.gsub(v.Text, "[^%d%.%-]", "")
				
	if v.Text:sub(1, 1) ~= "-" then
		v.Text = string.gsub(v.Text, "[^%d%.]", "")
	else
		v.Text = string.gsub(v.Text, "[^%d%.%-]", "")
	end
end)

(please consider “v” as the text box)

it has fewer bugs but if I type a minus in the 1st character position then I can place other minus wherever I want, at the end, etc

Solved,

I was trying to replace:

else v.Text = string.gsub(v.Text, "[^%d%.%-]", "")

with:

else v.Text = string.gsub(v.Text:sub(2), "[^%d%.%-]", "")

but then it removed the first character if it was a minus (couldn’t type a minus if it was the first character typed in the text box).

So I concatenated the first character with this gsub that is missing the first character:

v.Text = v.Text:sub(1,1) .. string.gsub(v.Text:sub(2), "[^%d%.]", "")

now the ifs and else’s aren’t even necessary, the whole code now is:

textBox:GetPropertyChangedSignal("Text"):Connect(function()
	textBox.Text = textBox.Text:sub(1,1) .. string.gsub(textBox.Text:sub(2), "[^%d%.]", "")
end)

(replaced the v’s with textBox so anyone having the same issue as me can visualise it better)

The only “issue” is that when you type a minus at the left of a number that’s not the 1st character your selection moves 1 character to the right, (not an issue for me). If anyone knows how to fix this, answer to this topic, but that’s the solution I found.

Thank you to everybody who answered to this topic!

1 Like

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