JackJack Posted May 14, 2006 Share Posted May 14, 2006 How do i get it to say"invalid username" if it contains symbol which isnt "a-Z 0-9 -_"?thank you Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 14, 2006 Share Posted May 14, 2006 use preg_match[code]<?php$username = "wildteend"; // returns TRUE - valid//$username = "wildteen88, Dan"; // returns False - invalid// check that username only containers characters from a-z in lower/upper case// and that the string only includes _ and - tooif(preg_match("/^[_a-zA-Z0-9-]+$/", $username)){ echo "You have a valid username!"; //TRUE}else{ echo "You have an invalid username!"; //FALSE}?>[/code][b]EDIT: Forgot to add in 0-9 in the expression. Thanks 448191 for spotting that out.Also sorted out a few problems too[/b] Quote Link to comment Share on other sites More sharing options...
448191 Posted May 14, 2006 Share Posted May 14, 2006 Only you also want to allow 0-9, so you have to add 0-9 between the square backets. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 14, 2006 Share Posted May 14, 2006 Umm, I must have misread the thread. Any whay I've added in 0-9 in to the regular expression now [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /] Quote Link to comment Share on other sites More sharing options...
toplay Posted May 14, 2006 Share Posted May 14, 2006 wildteen88, you don't need to backslash the dash in the character class when it's at the beginning or the end. Example:/[-a-zA-Z_]$/ or /[-a-zA-Z_-]$/ is fine.You forgot the 0-9 and the fact you can have more than one character. So, this expression would work where it expects at least one character/digit entered:/^[\w_-]+$/iSome flavors of regex engines may make \w to mean certain things. To be specific you could do the usual:/^[a-zA-Z0-9_-]+$/Edit:wildteen88 fixed the 0-9 but still missing ^ and a + or * otherwise it's just going to match one character - the last character since there's no ^ and there's a $. Quote Link to comment Share on other sites More sharing options...
448191 Posted May 14, 2006 Share Posted May 14, 2006 [!--quoteo(post=373773:date=May 14 2006, 11:44 AM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 14 2006, 11:44 AM) [snapback]373773[/snapback][/div][div class=\'quotemain\'][!--quotec--]wildteen88 fixed the 0-9 but still missing ^ and a + or * otherwise it's just going to match one character - the last character since there's no ^ and there's a $.[/quote]LOL, I missed that... Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 14, 2006 Share Posted May 14, 2006 Thanks toplay. I noticed that my expression wasn't working when I added in 0-9 and somei nvalud characters so I made a quick dash over to php.net and did a quick readup on a few pattern modifiersI have fixed my expression now. [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /] Quote Link to comment Share on other sites More sharing options...
toplay Posted May 14, 2006 Share Posted May 14, 2006 Hey, np.JackJack, if you want to insure the username starts with a letter, then use an expression something like this:/^[a-z][a-z0-9_-]*$/i [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /] Quote Link to comment Share on other sites More sharing options...
448191 Posted May 14, 2006 Share Posted May 14, 2006 Argh, I though the /i modifier was really a lot slower than a-zA-Z? [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /] Quote Link to comment Share on other sites More sharing options...
JackJack Posted May 14, 2006 Author Share Posted May 14, 2006 Thank you for all your help but how can i make it doesnt have one of those letters in it will come up with an error????Ty Quote Link to comment Share on other sites More sharing options...
haydndup Posted May 14, 2006 Share Posted May 14, 2006 What do you mean? the code that wildteen88 posted includes if statements to handle the response of the preg_match.[code]<?php$username = "wildteend"; // returns TRUE - valid//$username = "wildteen88, Dan"; // returns False - invalid// check that username only containers characters from a-z in lower/upper case// and that the string only includes _ and - tooif(preg_match("/^[_a-zA-Z0-9-]+$/", $username)){ echo "You have a valid username!"; //TRUE}else{ echo "You have an invalid username!"; //FALSE}?>[/code]Just change these parts:echo "You have a valid username!"; //TRUE...echo "You have an invalid username!"; //FALSE Quote Link to comment Share on other sites More sharing options...
JackJack Posted May 14, 2006 Author Share Posted May 14, 2006 I wish it were that simple.Heres the chunk of php im using.[code]}elseif(preg_match("/^[_a-zA-Z0-9-]+$/", $_POST['username'])){ echo ' <font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif"> • Invalid username: a-Z 0-9 -_ characters only </font>'; $show_form = 1;}[/code]$username = "test" would display error$username = "$}~##" wont display error:S Quote Link to comment Share on other sites More sharing options...
toplay Posted May 14, 2006 Share Posted May 14, 2006 [!--quoteo(post=373808:date=May 14 2006, 11:16 AM:name=JackJack)--][div class=\'quotetop\']QUOTE(JackJack @ May 14 2006, 11:16 AM) [snapback]373808[/snapback][/div][div class=\'quotemain\'][!--quotec--]I wish it were that simple.Heres the chunk of php im using.[code]}elseif(preg_match("/^[_a-zA-Z0-9-]+$/", $_POST['username'])){ echo ' <font color="#FF0000" size="2" face="Verdana, Arial, Helvetica, sans-serif"> • Invalid username: a-Z 0-9 -_ characters only </font>'; $show_form = 1;}[/code]$username = "test" would display error$username = "$}~##" wont display error:S[/quote]Put a ! before preg_match. Example:}elseif(!preg_match...Read up on basic logic and comparison operators.[a href=\"http://us2.php.net/manual/en/language.operators.logical.php\" target=\"_blank\"]http://us2.php.net/manual/en/language.operators.logical.php[/a] 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.