How to validate both Chinese (unicode) and English name?

Moon picture Moon · Jun 16, 2011 · Viewed 24.5k times · Source

I have a multilingual website (Chinese and English).

I like to validate a text field (name field) in javascript. I have the following code so far.

var chkName = /^[characters]{1,20}$/;

if( chkName.test("[name value goes here]") ){
  alert("validated");
}

the problem is, /^[characters]{1,20}$/ only matches English characters. Is it possible to match ANY (including unicode) characters? I used to use the following regex, but I don't want to allow spaces between each characeters.

/^(.+){1,20}$/

Answer

searlea picture searlea · Jun 16, 2011

You might check out Javascript + Unicode regexes and do some research to find exactly which ranges of characters you want to allow:

See What's the complete range for Chinese characters in Unicode?

After reading those two and a little extra research you should be able to find appropriate values to complete something like: /^[-'a-z\u4e00-\u9eff]{1,20}$/i