Jump to content

Preg_match


user4fun

Recommended Posts

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

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

You can also specify string length using regular expressions.

$pattern = "/^[a-zA-Z0-9]{7,12}$/";

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...