Page 1 of 1
$username validation
Posted: Sat Aug 30, 2008 3:00 am
by jsha
Ok Im doing a registration form validation for the username. I only want a-z, A-Z, 0-9, dashs and underscores. Any advise or changes are welcome.
Here it goes.
elseif(!preg_match('/^[-\w]*$/', $username) ) $err=$lang['err_signup17'];
I also searched all over the web for making this work with international characters if I want the site to be in other languages could not find anything can any way help with that please. Does \w apply to all international characters?
Re: $username validation
Posted: Sat Aug 30, 2008 3:22 am
by prometheuzz
jsha wrote:... Does \w apply to all international characters?
No, it only applies to the 8 bit ascii character set. You could try a regex like this:
Code: Select all
'/-_\d\p{L}/'u
// 'u' at the end is the unicode flag
// '\p{L}' is any unicode letter
Re: $username validation
Posted: Sat Aug 30, 2008 3:46 am
by jsha
prometheuzz wrote:jsha wrote:... Does \w apply to all international characters?
No, it only applies to the 8 bit ascii character set. You could try a regex like this:
Code: Select all
'/-_\d\p{L}/'u
// 'u' at the end is the unicode flag
// '\p{L}' is any unicode letter
Does \d represent any digit in Unicode?
Re: $username validation
Posted: Sat Aug 30, 2008 3:52 am
by prometheuzz
jsha wrote:...
Does \d represent any digit in Unicode?
No, that's also only the 0-9 from the ascii set. If you want unicode numbers, use
\p{N}
Re: $username validation
Posted: Sat Aug 30, 2008 3:54 am
by prometheuzz
Oh, and the regex I posted earlier, should have been this, of course:
Re: $username validation
Posted: Sat Aug 30, 2008 4:02 am
by jsha
prometheuzz wrote:Oh, and the regex I posted earlier, should have been this, of course:
So this should do the trick?
Re: $username validation
Posted: Sat Aug 30, 2008 4:21 am
by prometheuzz
No, this:
Code: Select all
'/^[-_\p{N}\p{L}]+$/'u // unicode letters, unicode numbers, underscores and hyphens
Re: $username validation
Posted: Sat Aug 30, 2008 4:27 am
by jsha
prometheuzz wrote:No, this:
Code: Select all
'/^[-_\p{N}\p{L}]+$/'u // unicode letters, unicode numbers, underscores and hyphens
Thanks Mate!
Re: $username validation
Posted: Sat Aug 30, 2008 4:34 am
by prometheuzz
jsha wrote:...
Thanks Mate!
You're welcome!