How to validate unicode characters?
I have one registra开发者_运维百科tion 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.
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';
}
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.
精彩评论