views:

78

answers:

2

Hello,

I have one registration form, I don't want people to register login username with unicode characters. How can i put server side validation PHP + client side validation javascript or jquery.

Please kindly help me out. Thank you.

A: 

A simple regular expression would do:

if (!username.match(/^[a-zA-Z0-9_+]+$)) {
   alert("invalid username");
}

This will check that the username contains only the characters a-z (upper and lower case), the digits 0-9, '_' and '+'. If you want to extend it to allow other characters, you'll just need to look up a bit more about regular expressions.

On the server-side, you'd do exactly the same thing, since PHP also supports regular expressions.

Dean Harding
thanks it worked.
morningglory
+1  A: 

Well do you mean outside of normal ASCII range?

You could implement one of the many seemsLikeUtf8() function floating around on the net.

Or do a quick regular expression

if ( ! preg_match('/^[a-z0-9-]+$/i', $username)) {
   echo 'error';
}
alex
Not sure if you realize this or not, but that pattern doesn't actually allow for UTF-8 characters.
Peter Bailey
thanks got it and it's worked. Thanks much
morningglory
@Peter The OP wanted to stop people registering using non standard characters I think. I'm not expert on UTF-8 (though learning) so feel free to point me in the right direction :)
alex
Wow, I managed to totally gloss over the "don't" in his question. Anyway, for the records, PCRE patterns can by made UTF-8 aware with the `u` flag (little u, not big U)
Peter Bailey
@Peter Thanks, I didn't know there was the little *u* flag. Just knew about the default ungreedy *U*. +1 to your comment because I learned something :)
alex