Page 1 of 1

username conditions

Posted: Thu Jul 16, 2009 9:24 am
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

Re: username conditions

Posted: Thu Jul 16, 2009 9:26 am
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?

Re: username conditions

Posted: Thu Jul 16, 2009 9:29 am
by WhizzBang
To get rid off spaces you could use trim() (i think) and for ' " could just use str_replace()

Re: username conditions

Posted: Thu Jul 16, 2009 12:55 pm
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";

Re: username conditions

Posted: Thu Jul 16, 2009 1:07 pm
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.

Re: username conditions

Posted: Thu Jul 16, 2009 1:19 pm
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...

Re: username conditions

Posted: Thu Jul 16, 2009 4:34 pm
by jackpf
Yeah...that's weird.

Stupid highlighter. Good examples btw.