Page 1 of 1

[solved] validating a post (checking for spaces in string)

Posted: Wed Jul 13, 2005 7:14 am
by robster
Hi all,

Just wondering what function I should be using to check if a username has spaces in it when a user registers on a form?

I've got this for the invalid characters:

Code: Select all

function check_username($str)
	{
		//returns 1 if valid email, 0 if not
		/* if(ereg("^.+@.+\..+$", $str))
			return 1;
		else
			return 0; */
		if (preg_match('/[a-z]+/i',$str)) 
		{
			return "1";
		}
		else
		{
			return "0";
		}
	}
but nothing for spaces it seems (I don't understand regular expressions sorry, no matter how many times I go over it, I AM an artist after all, just trying to figure this stuff out ;)).

I'd appreciate any direction.

Thanks,

Rob

Posted: Wed Jul 13, 2005 7:17 am
by timvw
You could test if there is something different than a-z, thus ^a-z..
Or you could make sure it has a-z from start to end, thus ^[a-z]$

Posted: Wed Jul 13, 2005 7:27 am
by robster
that would be this perhaps?

Code: Select all

if (preg_match('/^a-z..+/i',$str)) 
        {
            return "1";
        }
        else
        {
            return "0";
        }
I really am sorry, this is one area I just flop :(

I also don't understand what you mean by..
Or you could make sure it has a-z from start to end, thus ^[a-z]$
..and also (there's always another question ;)), I'd need them to have numerics also, ie: a-z and 0-9

I really appreciate this :?

Posted: Wed Jul 13, 2005 7:37 am
by Chris Corbyn

Code: Select all

if (preg_match('/^[a-z0-9]+$/i',$str)) {

   //Valid
}

Posted: Wed Jul 13, 2005 8:10 am
by robster
it worked a treat! I want to just thank you both :)