username conditions

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
sfoley77
Forum Newbie
Posts: 15
Joined: Tue Jun 02, 2009 10:13 am

username conditions

Post by sfoley77 »

I need to check a customers username and write a function so the user name may include punctuation characters but cannot contain spaces or single or double apostrophes.
Would really apprechiate some help on this thanks in advance

Sean
aliciadg
Forum Newbie
Posts: 16
Joined: Tue Jul 14, 2009 3:39 pm

Re: username conditions

Post by aliciadg »

So your script logic would be:
if username=some character set AND if username does not contain quotes or blank spaces?
Is this correct? Do you have any code written so far?
WhizzBang
Forum Newbie
Posts: 11
Joined: Sat Jul 11, 2009 5:39 pm

Re: username conditions

Post by WhizzBang »

To get rid off spaces you could use trim() (i think) and for ' " could just use str_replace()
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: username conditions

Post by Ollie Saunders »

sfoley77, look to regular expressions, in PHP these are implemented in the preg_*() functions. Also the ctype_*() functions are good too.

@Whizzbang: trim() removes spaces from either side of a string. It doesn't so much check for the existence of spaces nor does it care about ones appearing in between non-space characters.

Code: Select all

trim(" f o o ") == "f o o";
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: username conditions

Post by jackpf »

You could do something like this

if(preg_match('/(\s|["\'])/', $username))
{
echo 'invalid username';
}

Wtf this is stupid; the syntax highlighter keeps removing my escaped characters. I had to remove the code tags...I've reported this but had no response as yet.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: username conditions

Post by requinix »

The question is whether there's a list of allowed characters (with punctuation)

Code: Select all

$valid = preg_match('/^[a-zA-Z0-9!:;,.?...]$/', $text);
or a list of disallowed characters (with spaces and quotes)

Code: Select all

$valid = preg_match('/^[^\s"\x27]$/', $text);
The former restricts the username to a specific list of characters; the latter allows any character except for a small handful.
jackpf wrote:Wtf this is stupid; the syntax highlighter keeps removing my escaped characters. I had to remove the code tags...I've reported this but had no response as yet.
It's been doing that for a long time.

Code: Select all

'\'' // odd coloring? nope: an escaped apostrophe with an invisible backslash
It's just the highlighter though: the backslash is still there - try quoting me...
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: username conditions

Post by jackpf »

Yeah...that's weird.

Stupid highlighter. Good examples btw.
Post Reply