How to check if TextBox only contains Letters?

Surprisingly, I couldn’t find any resources on this topic. I am current creating a system where you can select your own Name inside of the game, but I ran into this issue of how to check if the Name only contains letters. Any help would be appreciated.

1 Like

You can use string.match() for this.

local str = "testTEST test"

if string.match(str, "[%a%s]+") == str then
	--@ only letters, and spaces
	warn("valid")
else
	--@ anything else
	warn("invalid")
end

I think it’d be better just to check for anything EXCEPT letters:

local str = "testus3rname"
if str:match("[^a-zA-Z]+") then
    -- oh no guys it has forieng entities!!
else
    -- no special characters or numbers
end

Of course, this fully depends on what the OP wants. This version does seem to cut down on length a bit, but I’m not sure about performance (it’s regex, so it should be pretty good.)

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