0

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/

Sriks
  • 1,669
  • 6
  • 26
  • 40
  • It may be easier to whitelist characters you want to allow rather than blacklisting huge portions of _UTF_ – Paul S. Dec 30 '14 at 11:35
  • show this => http://stackoverflow.com/questions/8340719/regex-to-remove-non-letter-characters-but-keep-accented-letters – Abdallah Arffak Dec 30 '14 at 11:38
  • What does "other language" mean in the context of a multi-lingual site? Not English? – Álvaro González Dec 30 '14 at 12:22
  • I mean chinese, spanish, etc., – Sriks Dec 30 '14 at 12:32
  • Yeah, right, those are human languages. What languages are *not* acceptable for you app? And... why? Is it okay if I type a Spanish sentence that only has English characters? What's the ultimate goal? – Álvaro González Dec 30 '14 at 12:47
  • Yes, when I am in en-GB culture, my app should not allow spanish, chinese characters. when I am in zn-TW culture, it can allow enligsh. Thats not a problem. – Sriks Dec 30 '14 at 12:51
  • @Sriks. Remember that English also borrows words from other languages. `apartheid` for example. Be very careful with restricting characters. – Mouser Jan 01 '15 at 09:32
  • @Mouser. Ok, Thanks once agiain :) – Sriks Jan 01 '15 at 10:53

1 Answers1

1

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?

Community
  • 1
  • 1
Mouser
  • 13,132
  • 3
  • 28
  • 54
  • Here even if I enter, "as" as text, it alerts as "Chinese character sets".. I have tried for "as供應商", this also alerts the same. – Sriks Dec 30 '14 at 13:02
  • Hi Mouser, I have tried. Not working :( Please find the fiddle: http://jsfiddle.net/srikanth1818/ofkhsr9x/ – Sriks Dec 30 '14 at 16:02
  • @Sriks It's working perfectly for me now, deleted beginning `^` and end `$` of string: http://jsfiddle.net/ofkhsr9x/1/ – Mouser Dec 31 '14 at 14:05
  • Thanks @Mouser.. Its working by deleting ^ and $. Thanks a lot :) – Sriks Jan 01 '15 at 09:20