user4fun Posted April 10, 2009 Share Posted April 10, 2009 I give up, this is self explanatory but I cannot get teh preg match to work. I want the user to be able to use capital leterssmall lettersan numbers only. Anything else will be rejected. $pattern = "/^([a-zA-Z0-9])+$/"; if(preg_match($pattern,$myusername)) { $objValue_length = strlen($myusername); if (($objValue_length>=7) && ($objValue_length<=12)) { echo "Available username"; } else { echo "It must be more than 7 and less than 12 characters."; } } else { echo "You cannot use special Characters"; } Link to comment Share on other sites More sharing options...
jesh Posted April 10, 2009 Share Posted April 10, 2009 I'm not too familiar with PHP's implementation of regular expressions, but, since you aren't really doing any grouping, I think you should lose the parentheses. Your expression is looking for one or more groups of single characters rather than one or more characters. $pattern = "/^[a-zA-Z0-9]+$/"; You could also search for any character that does not match your requirements. If there is a match here, the string is not valid: $pattern = "/[^a-zA-Z0-9]/"; Link to comment Share on other sites More sharing options...
Synook Posted April 11, 2009 Share Posted April 11, 2009 You can also specify string length using regular expressions. $pattern = "/^[a-zA-Z0-9]{7,12}$/"; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.