Sebery
|
  |
| Joined: 05 Apr 2014 |
| Total Posts: 182 |
|
|
| 06 Nov 2015 03:03 PM |
Hey, i wanted to ask on how do i make a TextBox ( Instance.new('TextBox') ) allows only certain characters. Like:
local charTable = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
So if you enter in the Textbox now the Letter "A" it wont appear, and if u enter the Number "5" it appears in the Textbox. How do i do that?
Greetings, Sebery. |
|
|
| Report Abuse |
|
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
|
| 06 Nov 2015 03:06 PM |
put localscript into textbox:
script.Parent.Changed:connect(function(prop) if prop ~= "Text" then return end script.Parent.Text = script.Parent.Text:gsub("%d","") end) |
|
|
| Report Abuse |
|
|
DrHaximus
|
  |
| Joined: 22 Nov 2011 |
| Total Posts: 8410 |
|
|
| 06 Nov 2015 03:06 PM |
if(str:match("%W")) then -- has numbers end |
|
|
| Report Abuse |
|
|
Sebery
|
  |
| Joined: 05 Apr 2014 |
| Total Posts: 182 |
|
|
| 06 Nov 2015 03:08 PM |
Ehh, wat?
Greetings, Sebery. |
|
|
| Report Abuse |
|
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
| |
|
Sebery
|
  |
| Joined: 05 Apr 2014 |
| Total Posts: 182 |
|
|
| 06 Nov 2015 03:11 PM |
Not that e.e
Greetings, Sebery. |
|
|
| Report Abuse |
|
|
DrHaximus
|
  |
| Joined: 22 Nov 2011 |
| Total Posts: 8410 |
|
|
| 06 Nov 2015 03:13 PM |
In my example, str would be a string which you would get with
textbox.Text |
|
|
| Report Abuse |
|
|
Sebery
|
  |
| Joined: 05 Apr 2014 |
| Total Posts: 182 |
|
|
| 06 Nov 2015 03:14 PM |
This works, but the Problem is, it only allows Letters. I only want it to allow Numbers and dots (.)
Greetings, Sebery. |
|
|
| Report Abuse |
|
|
DrHaximus
|
  |
| Joined: 22 Nov 2011 |
| Total Posts: 8410 |
|
|
| 06 Nov 2015 03:15 PM |
Just do if (tonumber(textbox.Text)~=nil) then -- valid end |
|
|
| Report Abuse |
|
|
DrHaximus
|
  |
| Joined: 22 Nov 2011 |
| Total Posts: 8410 |
|
|
| 06 Nov 2015 03:16 PM |
| I forgot to mention that I thought you meant only allow non-numerical characters. Use my latest example. |
|
|
| Report Abuse |
|
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
|
| 06 Nov 2015 03:17 PM |
script.Parent.Changed:connect(function(prop) if prop ~= "Text" then return end script.Parent.Text = script.Parent.Text:gsub("%D","") end)
did not hold down shift |
|
|
| Report Abuse |
|
|
Sebery
|
  |
| Joined: 05 Apr 2014 |
| Total Posts: 182 |
|
|
| 06 Nov 2015 03:19 PM |
So :gsub("%d","") allows only Letters and :gsub("%D","") allows only Numbers? Wow
Oh and, thanks. Now i need to allow it, that i can enter things like "5.2" and not just "52"
Greetings, Sebery. |
|
|
| Report Abuse |
|
|
DrHaximus
|
  |
| Joined: 22 Nov 2011 |
| Total Posts: 8410 |
|
|
| 06 Nov 2015 03:22 PM |
| Use my example, it allows decimal or float numbers (numbers with periods). |
|
|
| Report Abuse |
|
|
Sebery
|
  |
| Joined: 05 Apr 2014 |
| Total Posts: 182 |
|
|
| 06 Nov 2015 03:23 PM |
Give me a complete Script please. Not just a part since i dont know on how2work with these functions e.e
Greetings, Sebery. |
|
|
| Report Abuse |
|
|
DrHaximus
|
  |
| Joined: 22 Nov 2011 |
| Total Posts: 8410 |
|
|
| 06 Nov 2015 03:25 PM |
If you want to build it onto from the other person's example, do something like this:
script.Parent.Changed:connect(function(prop) if prop ~= "Text" then return end if(tonumber(script.Parent.Text)~=nil) then script.Parent.Text = tonumber(script.Parent.Text) end end)
do some error handling or whatever |
|
|
| Report Abuse |
|
|
Zaxerion
|
  |
| Joined: 09 Feb 2013 |
| Total Posts: 2304 |
|
|
| 06 Nov 2015 03:28 PM |
local Characters = { "1" = true, "2" = true, "3" = true, "4" = true, "5" = true, "6" = true, "7" = true, "8" = true, "9" = true, "0" = true, "." = true, }
local Last
script.Parent.Changed:connect(function(Property) if Property == "Text" then for i = 1, #script.Parent.Text do if not Characters[script.Parent.Text:sub(i, i)] then script.Parent.Text = Last end end Last = script.Parent.Text end end)
A bit inefficient, but allows for decimals.
Currently developing a game with Scripters Interactive! Join the group to show support! |
|
|
| Report Abuse |
|
|
rayk999
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 4705 |
|
|
| 06 Nov 2015 03:32 PM |
script.Parent.Changed:connect(function() local numberify = tonumber(script.Parent.Text)
if not numberify then script.Parent.Text = script.Parent.Text:sub(1,#script.Parent.Text-1) else script.Parent.Text = numberify end end) |
|
|
| Report Abuse |
|
|
Sebery
|
  |
| Joined: 05 Apr 2014 |
| Total Posts: 182 |
|
|
| 06 Nov 2015 03:39 PM |
I maked it a bit better:
local Characters = { ["1"] = true, ["2"] = true, ["3"] = true, ["4"] = true, ["5"] = true, ["6"] = true, ["7"] = true, ["8"] = true, ["9"] = true, ["0"] = true, ["."] = true, }
local Last
script.Parent.Changed:connect(function(Property) if Property == "Text" then for i = 1, #script.Parent.Text do if Characters[script.Parent.Text:sub(i, i)] then -- Nothing, skrub. else local len = script.Parent.Text:len() script.Parent.Text = script.Parent.Text:sub(1, len-1) end end Last = script.Parent.Text end end)
Fully works, thanks.
Greetings, Sebery. |
|
|
| Report Abuse |
|
|
|
| 06 Nov 2015 03:42 PM |
function AllowedCharacters = { ["1"]=true, ["2"]=true, ["3"]=true, ["4"]=true, ["5"]=true, ["6"]=true, ["7"]=true, ["8"]=true, ["9"]=true, ["0"]=true, ["."] = true }
script.Parent.Changed:connect(function() if not AllowedCharacters[script.Parent.Text:sub(-1,-1)] then script.Parent.Text = script.Parent.Text:sub(1,-2) end end)
Like that? |
|
|
| Report Abuse |
|
|
Sebery
|
  |
| Joined: 05 Apr 2014 |
| Total Posts: 182 |
|
|
| 06 Nov 2015 03:42 PM |
Use the one i posted (above your Post)
Greetings, Sebery. |
|
|
| Report Abuse |
|
|
Zaxerion
|
  |
| Joined: 09 Feb 2013 |
| Total Posts: 2304 |
|
|
| 06 Nov 2015 03:52 PM |
You didn't make it better, you changed it to your preferred way.
Currently developing a game with Scripters Interactive! Join the group to show support! |
|
|
| Report Abuse |
|
|
|
| 06 Nov 2015 05:17 PM |
| Use mine, it's more efficient. |
|
|
| Report Abuse |
|
|