如何使用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.

小提琴:

$(".textTest input").keyup(function () {如果 ($(this).val().match(/^[\u4E00-\u9FFF\u3400-\u4DFF\uF900-\uFAFF]+$/g)) {alert('中文字符集');}else alert('英语');});

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

您使用正则表达式的方向是正确的.下面的代码检查 Unicode 中的中文字符集.

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:

Unicode 中汉字的完整范围是多少?