1

How can I set the word limit to 200 and not allow input and show a warning next to the text area when the limit is reached using jQuery? The user may input Chinese or English characters.

Are Chinese and English counted in the same way?

I just used the build in function set the length[1,200]

Jack Bonneman
  • 1,821
  • 18
  • 24
user2210819
  • 145
  • 2
  • 14

3 Answers3

0

I don't know if Chinese will work or not, but you might do this:

HTML

<input type="text" name="input" />
<span></span>

jQuery

$(function(){
    $('input[name="input"]').keyup(function(){
        if($(this).val().length > 10){
            $('span').html('You can\'t type more than 10 characters!');
        }else{
            $('span').empty();
        }
    });
});

See the result here (just change 10 to 200).

tshepang
  • 12,111
  • 21
  • 91
  • 136
Aldi Unanto
  • 3,626
  • 1
  • 22
  • 30
0

It says something related to Chinese but doesn't work Chinese limit for me.

jQuery Text Counter Plugin

Web_Developer
  • 1,251
  • 2
  • 18
  • 34
0

Here is a simple answer. idea is from This GitHub Repo
Chinese and English aren't counted in the same way,if you count it by byteLength , you will find that chinese is 2 times of english you can try code below .

/*useByteLength
function checkTextLength($element) {
  txtCount = $element.val().replace(/[^\x00-\xff]/g, "pp").length + 
  ($element.val().match(/\n/g) || []).length;
  return txtCount;
}

if your condition need to count Chinese and English in the same way , you can try this.

 /*count Chinese and English  in the same way
 function checkTextLength($element) {
  txtCount = $elements.val().length + ($elements.val().match(/\n/g) || []).length;
  return txtCount; 
}