Sriks
Sriks

Reputation: 1699

How to restrict Chinese characters in a textbox using javascript

I have a multilingual website, where I want to restrict typing of other language characters (especially Chinese) using a regular expression. Can someone please give me an example how to implement this.

Fiddle:

$(".textTest input").keyup(function () { if ($(this).val().match(/^[\u4E00-\u9FFF\u3400-\u4DFF\uF900-\uFAFF]+$/g)) { alert('Chinese character sets'); } else alert('English'); });

http://jsfiddle.net/srikanth1818/ofkhsr9x/

Upvotes: 0

Views: 5508

Answers (1)

Mouser
Mouser

Reputation: 13304

You were going in the right direction with the regex. This code below checks for the Chinese characters sets in Unicode.

$(".textTest input").keyup(function () {
    if ($(this).val().match(/[\u4E00-\u9FFF\u3400-\u4DFF\uF900-\uFAFF]+/g)) {
        alert('Chinese character sets');
    }
    else alert('English');
});

More information can be found here:

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

Upvotes: 1

Related Questions