How to detect special characters in a textbox?

I want to detect special characters such as $#@ in a textbox, how can I do this?

local aString = "abc123!@#"
local specialString = string.gsub(aString, "%w", "")
print(specialString)
--!@# is output

In your context, something like:

local textbox = script.Parent
local specialCharsFromString = string.gsub(textbox.Text, "%w", "")
--do code

I’m trying to filter them out, so would I do

if string.gsub(textbox.Text, "%w", "") then
local textbox = script.Parent
local specialCharsFromString = string.gsub(textbox.Text, "[^%w]", "")
print(specialCharsFromString)
textbox.Text = specialCharsFromString
local textbox = script.Parent
local specialCharsFromString = string.gsub(textbox.Text, "%W", "")
print(specialCharsFromString)
textbox.Text = specialCharsFromString

This will return true if 1 alphanumeric character is found in the string, regardless of whether or not special characters are present.

if string.match(textbox.Text, "^%w$") then

This would work however.

1 Like

Here is my current script, when I do $$$$ nothing happens.

	elseif string.match(Text, "^%w$") then
		script.Parent.Parent.Warning.Visible = true
		script.Parent.Parent.Warning.Text = "Warning: Cannot have any special characters"
		wait(3)

		script.Parent.Parent.Warning.Visible = false
	else
if string.match(textbox.Text, "^%w$") then

This matches if the string starts and ends with only alphanumeric characters. If you want to match a string which has 1 or more special characters use the following:

if string.match(textbox.Text, "%W") then
elseif string.match(textbox.Text, "%W") then
	script.Parent.Parent.Warning.Visible = true
	script.Parent.Parent.Warning.Text = "Warning: Cannot have any special characters"
	wait(3)
	script.Parent.Parent.Warning.Visible = false
else
2 Likes