Corangar Posted March 8, 2007 Share Posted March 8, 2007 Here is the deal i searched this forum and found a similar topic. But the script didn't work for me and i dont wanna weak up the ghosts if (!preg_match("/[a-z0-9]/i", $username){ print "Username field has invalid characters!"; exit; } Thats a code from "HuggieBear" The thing is, script works for me in a way that it detects invalid characters but ONLY if all characters are invalid. If i would insert as username "##$$" it would print "Username field has invalid characters!" But if i insert "##C2" it would consider it as a valid character cuz of C and 2 With other words, i am searching for a script that would detect if it has ANY invalid character anywhere, no matter if valid characters were inserted. Any help would be nice thanks Quote Link to comment Share on other sites More sharing options...
obsidian Posted March 8, 2007 Share Posted March 8, 2007 A slight alteration to the regexp will solve that. You need to tell the regexp engine to require all the characters to be within your ranges provided: <?php if (!preg_match("/^[a-z0-9]+\z/i", $username)) { // Your username contains invalid characters } ?> Or, you could simply match for any occurrence of a character not within your range (this may be better): <?php if (preg_match('|[^a-z0-9]|i', $username)) { // Your username contains invalid characters } ?> Quote Link to comment Share on other sites More sharing options...
rantsh Posted March 8, 2007 Share Posted March 8, 2007 Do you need to use preg? I guess ereg has is an easier function I guess you could use something like if(ereg("[a-z]|[A-Z]|[0-9]";$var)) { //valid } else { //invalid } That'll look for any alpha-num char and return true if present. Hope I was helpful, and btw, I guess this topic goes on the Regex forum... Regards, Roderick Quote Link to comment Share on other sites More sharing options...
effigy Posted March 8, 2007 Share Posted March 8, 2007 Do you need to use preg?...I guess you could use something like ...[a-z]|[A-Z]|[0-9]... Per the docs: Note: preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg(). Also, never alternate like that--combine the class: [A-Za-z0-9] Quote Link to comment Share on other sites More sharing options...
Corangar Posted March 8, 2007 Author Share Posted March 8, 2007 <?php if (!preg_match("/^[a-z0-9]+\z/i", $username)) { // Your username contains invalid characters } ?> This works perfectly, thank you all Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.