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

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
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

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

Post 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
Last edited by robster on Wed Jul 13, 2005 8:10 am, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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]$
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post 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 :?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

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

   //Valid
}
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

it worked a treat! I want to just thank you both :)
Post Reply