Checking for Spaces

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sampage
Forum Newbie
Posts: 22
Joined: Sat Mar 18, 2006 6:17 pm

Checking for Spaces

Post by sampage »

This seems like a very simple process but I am obviously doing something wrong.

I just want to check that when a user enters a username they only use set characters and no spaces

Here's the validation code I am using:

Code: Select all

if(!preg_match('/[^a-zA-Z\_\-0-9]+$/',$username)) 
{
$message="Your username contains illegal characters on spaces.";
	unset($username); 
    include 'index.php'; // Show the form again! 
    exit();
}
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: Checking for Spaces

Post by jmut »

sampage wrote:This seems like a very simple process but I am obviously doing something wrong.

I just want to check that when a user enters a username they only use set characters and no spaces

Here's the validation code I am using:

Code: Select all

if(!preg_match('/[^a-zA-Z\_\-0-9]+$/',$username)) 
{
$message="Your username contains illegal characters on spaces.";
	unset($username); 
    include 'index.php'; // Show the form again! 
    exit();
}

Code: Select all

if(!preg_match('/^[a-zA-Z_\-0-9]+$/',$username)) 
{
$message="Your username contains illegal characters on spaces.";
	unset($username); 
    include 'index.php'; // Show the form again! 
    exit();
}
Is this what you are trying to do.
If you don't put ^ at the begginning and $ at the end of reg exp. there might be additional symbols, hence breaking the required rule
sampage
Forum Newbie
Posts: 22
Joined: Sat Mar 18, 2006 6:17 pm

Post by sampage »

Thanks for that! I new it was something that I was missing but after hours of coding couldn't spot it!

I love this forum more and more!
Post Reply