Wednesday, September 2, 2015

Javascript detect Chinese Character

Here is the function to detect Chinese Chracters. However, we need do more research about CJK (Chinese, Japanese, Korean) about the unicode character range.

function isChineseCharacter(str) {

    var re1 = new RegExp("^[\u4E00-\uFA29]*$"); //Chinese character range
    var re2 = new RegExp("^[\uE7C7-\uE7F3]*$"); //non Chinese character range
    str = str.replace(/\s/g, '');

    if (!re1.test(str) || re2.test(str)) {
        console.log("Oops, Please input Chinese character.");
        return false;
    }

    console.log('Chinese character');
    return true;
}

3 comments:

  1. http://www.kidslovepc.com/javascript/javascript-detect-chinese-input.shtml

    ReplyDelete
  2. The code is from above link, but not sure of the character code range.

    ReplyDelete