I have a function where I need to check for special characters and break it if I find one.
Here is what I have tried.
local text = "h!ello\"wor%[email protected]^*sp&ki#$te"
if (string.match(text, "&") ~= nil) then
print("Invalid Character \n")
end
if (string.match(text, "\"") ~= nil) then
print("Invalid Character \n")
end
if (string.match(text, "#") ~= nil) then
print("Invalid Character \n")
end
if (string.match(text, "$") ~= nil) then
print("Invalid Character \n")
end
if (string.match(text, "@") ~= nil) then
print("Invalid Character \n")
end
if (string.match(text, "%%") ~= nil) then
print("Invalid Character \n")
end
if (string.match(text, "!") ~= nil) then
print("Invalid Character \n")
end
if (string.match(text, "^") ~= nil) then
print("Invalid Character \n")
end
if (string.match(text, "*") ~= nil) then
print("Invalid Character \n")
end
I'm able to successfully do this, but I want everything in single line. Went through Lua Programming book but couldn't get.
To match on one of a set of characters you need to use the
[set]
notation1.So the following should do what you want and provide a slightly better indication of the failure:
You could even include the exact character that failed: