Page 1 of 1

Checking for Spaces

Posted: Sun Mar 19, 2006 6:35 am
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();
}

Re: Checking for Spaces

Posted: Sun Mar 19, 2006 6:52 am
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

Posted: Sun Mar 19, 2006 7:23 am
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!