Question

I'm using a keyup() event on a textbox, which sends an AJAX request to a php file. The input is usually in Chinese/Korean/Japanese text. The problem is that the IME for these languages is based on Latin, then you press a number, and it converts it to the character. E.g.,

enter image description here

Even though the Latin doesn't show up in the textbox at all, each letter is sent as an AJAX request, so it must be there but "invisible" or something. For example, if I type "ni hao", the following are sent as requests:

  1. n
  2. ni
  3. ni h
  4. ni ha
  5. ni hao
  6. 你好

Even though only 你好 is ever shown in the textbox. This results in so many wasted AJAX queries, so how would I go about limiting the queries to the chinese characters only?

Was it helpful?

Solution

This is really inelegant, but maybe you could check the last inputted character's character code and see if it is higher than the character code for English characters (usually 60-100):

 $('textarea').on('keyup',function(e) {
    var value = $(this).val(),
        length = value.length,
        lastChar = value.substring(length-1, length),
        charCode = lastChar.charCodeAt(0);

    if (charCode > 200) {
        // $.ajax()  
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top