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

25,019

Solution 1

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

Solution 2

Take a look at Regex Unicode blocks.

You can use this to take care of CJK names.

Share:
25,019
Moon
Author by

Moon

Updated on August 21, 2020

Comments

  • Moon
    Moon almost 4 years

    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}$/
    
  • talltolerablehuman
    talltolerablehuman over 5 years
    in case ie.: german äüöß, french é..., spanish ñ... should be supported, the regex would need to be extended