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
username conditions
Moderator: General Moderators
Re: username conditions
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?
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
To get rid off spaces you could use trim() (i think) and for ' " could just use str_replace()
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: username conditions
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.
@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
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.
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
The question is whether there's a list of allowed characters (with punctuation)
or a list of disallowed characters (with spaces and quotes)
The former restricts the username to a specific list of characters; the latter allows any character except for a small handful.
It's just the highlighter though: the backslash is still there - try quoting me...
Code: Select all
$valid = preg_match('/^[a-zA-Z0-9!:;,.?...]$/', $text);Code: Select all
$valid = preg_match('/^[^\s"\x27]$/', $text);It's been doing that for a long time.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.
Code: Select all
'\'' // odd coloring? nope: an escaped apostrophe with an invisible backslashRe: username conditions
Yeah...that's weird.
Stupid highlighter. Good examples btw.
Stupid highlighter. Good examples btw.