DID My Home Work Can You Please Check.

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

DID My Home Work Can You Please Check.

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
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: DID My Home Work Can You Please Check.

Post by greyhoundcode »

I didn't realise that \w represented all alphanumeric characters. Handy to know. Even so, for clarity when I'm debugging I would probably stick to something more plain vanilla:

Code: Select all

/^[a-zA-Z0-9_\-]+$/
As far as your internationalisation goes, an equivalent function to ereg_match() is mb_ereg_match() which, as the prefix mb_ suggests, has multi-byte support and thus will work with various international character sets.

As I'm sure you noticed it's a POSIX regex not a PCRE, so, and I haven't tested this, if you use mb_ereg_match() you ought to remove your delimiters:

Code: Select all

^[a-zA-Z0-9_\-]+$
Post Reply