Jump to content

simple preg_match question


arbitter

Recommended Posts

Hello,

 

When a user registers, he is only allowed to register a name containing a-z, A-Z, and 0-9.

Now I have searched for hours for decent information about preg_match(), but I jsut don't understand what I need to do.

 

let's say the username = $nick

 

I had:

if(!preg_match('/[A-Za-z0-9]/',$nick))
		{
			echo 'Username is not allowed.';
		}

 

I thought this was the correct solution, but now I noticed; preg_match only goes to the first letter that is in the array and then outputs a 1.

I've searched dozens of sites and can't find out how to do this;

how do I make it check every character in $nick?

 

I know this is probably a veeeryyy simple question, but I just can't figure it out.

Link to comment
https://forums.phpfreaks.com/topic/201106-simple-preg_match-question/
Share on other sites

@arbitter

All your pattern does is check if the string contains one of those characters. What JAY6390 has done is add the ^ and $ to anchor the pattern at the start and end of the string respectively and added the + which is a quantifier that says 1 or more. Therefore the pattern will match if the string is one or more characters that only contains those letters/digits.

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.