如何使用javascript限制文本框中的汉字 [英] How to restrict Chinese characters in a textbox using javascript
本文介绍了如何使用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:
这篇关于如何使用javascript限制文本框中的汉字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文