Jump to content

[Help] Use allowed characters only


3NAD

Recommended Posts

Hello,

How can i make a String which can be accepted with English letters and Numbers only.

For example:

AllowedLetters = {
	"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
	"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
	"U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4",
	"5", "6", "7", "8", "9", "0", "_"
}

MyString = "3NAD #"
-- it has '#' so its not allowed.

And thank you.

 

Edited by 3NAD
Link to comment

Something like this should work (I'm sure there are much better ways lol) Like using regex

Also your character set is very limited, what are you going to use it for? It doesn't have characters like ? ! : ( ) , . ; etc which will be used often, it will be too much anyway and you're gonna have to use regex to replace the strings properly. Unfortunately I'm bad with it, so this is all I have for you:

AllowedLetters = {
	"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
	"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
	"U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4",
	"5", "6", "7", "8", "9", "0", "_", " "
}

function isStringValid(str)
	local ch = str
	for _,char in pairs(AllowedLetters) do
		ch = string.gsub(ch:upper(),char,"")
	end	
	
	iprint(str," matched: ",ch)
	if #ch < 1 then
		return str
	else
		return false
	end
end

I added a space to the table " ", remove it if you don't want them.

Another way is to make an illegal character set and then remove those with string.gsub or again using regex

Edited by Tails
  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...