3NAD Posted November 3, 2016 Share Posted November 3, 2016 (edited) 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 November 3, 2016 by 3NAD Link to comment
Tails Posted November 3, 2016 Share Posted November 3, 2016 (edited) 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 November 3, 2016 by Tails 1 Link to comment
Walid Posted November 3, 2016 Share Posted November 3, 2016 Try this local myString = "3NAD #" local result = (myString:find("%W") == nil) and "Allowed" or "Not allowed" outputChatBox(result) 1 Link to comment
Popular Post zneext Posted November 3, 2016 Popular Post Share Posted November 3, 2016 The with-regex way: function isStringValid ( str ) return not pregFind ( str, "[^a-zA-Z0-9_]" ); end 5 Link to comment
3NAD Posted November 3, 2016 Author Share Posted November 3, 2016 @Tails @Walid @znext All of these codes are working fine, but i chose the 'regex' one. Thanks for all. 1 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now