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

前端 未结 5 1313
说谎
说谎 2021-02-01 10:14

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 c         


        
相关标签:
5条回答
  • 2021-02-01 10:30

    I have done some work on validating Chinese names using XRegExp. The core code is XRegExp("^((?![\\p{InKangxi_Radicals}\\p{InCJK_Radicals_Supplement}\\p{InCJK_Symbols_and_Punctuation}])\\p{Han}){2,4}$","u")

    See jsfiddle.net/coas/4djhso1y

    0 讨论(0)
  • 2021-02-01 10:37

    Take a look at Regex Unicode blocks.

    You can use this to take care of CJK names.

    0 讨论(0)
  • 2021-02-01 10:38
    var chkName = /\s/;
    
    function check(name) {
    
        document.write("<br />" + name + " is ");
    
        if (!chkName.test(name)) {
            document.write("okay");
        } else {
            document.write("invalid");
        }
    
    }
    
    check("namevaluegoeshere");
    
    check("name value goes here");
    

    This way you just check if there's any white space in the name.

    demo @ http://jsfiddle.net/roberkules/U3q5W/

    0 讨论(0)
  • 2021-02-01 10:46

    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

    0 讨论(0)
  • 2021-02-01 10:47

    As of 2018, there is new syntax in JavaScript to match Chinese or any other non-ASCII scripts:

    const REGEX = /(\p{Script=Hani})+/gu; // note the 'u'
    '你好'.match(REGEX);
    // ["你好"]
    

    The trick is to use \p and use the right script name, Hani stands for Han script (Chinese). The full list of scripts is here: http://unicode.org/Public/UNIDATA/PropertyValueAliases.txt

    To match both Chinese and English you just expand it a bit, for example:

    const REGEX = /([A-Za-z]|\p{Script=Hani})+/gu;
    // does not match accented letters though
    
    0 讨论(0)
提交回复
热议问题