$username validation

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
jsha
Forum Newbie
Posts: 5
Joined: Fri Aug 29, 2008 7:27 pm

$username validation

Post 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?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: $username validation

Post 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
jsha
Forum Newbie
Posts: 5
Joined: Fri Aug 29, 2008 7:27 pm

Re: $username validation

Post 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?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: $username validation

Post 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}
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: $username validation

Post by prometheuzz »

Oh, and the regex I posted earlier, should have been this, of course:

Code: Select all

'/^[-_\d\p{L}]+$/'u
jsha
Forum Newbie
Posts: 5
Joined: Fri Aug 29, 2008 7:27 pm

Re: $username validation

Post by jsha »

prometheuzz wrote:Oh, and the regex I posted earlier, should have been this, of course:

Code: Select all

'/^[-_\d\p{L}]+$/'u
So this should do the trick?

Code: Select all

'/^[-_\d\p{N}]+$/'u
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: $username validation

Post by prometheuzz »

No, this:

Code: Select all

'/^[-_\p{N}\p{L}]+$/'u // unicode letters, unicode numbers, underscores and hyphens
jsha
Forum Newbie
Posts: 5
Joined: Fri Aug 29, 2008 7:27 pm

Re: $username validation

Post by jsha »

prometheuzz wrote:No, this:

Code: Select all

'/^[-_\p{N}\p{L}]+$/'u // unicode letters, unicode numbers, underscores and hyphens

Thanks Mate!
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: $username validation

Post by prometheuzz »

jsha wrote:...
Thanks Mate!
You're welcome!
Post Reply