4

I've an textarea with a specific width and height so how do I create a letter counter and set the maximum letters typed in the textarea should not be more than 100 words.

HTML

<div class="box">
<textarea name="" id=""></textarea>
<div class="word-counter">0/100</div>
</div>

CSS

.box {
width:500px;
border: 1px solid red;
position:relative;
display:table;
}
textarea {
width:500px;
height:80px;
padding-bottom: 20px;
}
.word-counter {
position:absolute;
bottom: 0;
right:0;
}
Firnas
  • 379
  • 2
  • 8
  • 24
  • similar functionality has been discussed here: http://stackoverflow.com/questions/30332301/jquery-textarea-character-countdown-on-page-load/30332436#30332436 – JGV May 20 '15 at 05:15

5 Answers5

3

Use .keyup() for counting letters.

$('#area').keyup(function(){
    $('.word-counter').text($.trim(this.value.length)+'/100');
})

And maxlength attribute for max letters.

<div class="box">
<textarea name="" id="area" maxlength="100"></textarea>
<div class="word-counter">0/100</div>
</div>

Demo Fiddle

*Note : This supports spaces. If you need to handle just letter count the keyup() can be manipluated.

$('#area').keyup(function(){
    $('.word-counter').text(this.value.replace(/ /g,'').length+'/100');
})

Fiddle (Spaces ignored in letter count)

Shaunak D
  • 20,588
  • 10
  • 46
  • 79
3

You could simply use maxlength but, this means you are placing a character limit not a word limit on it. If you are asking for a word limit you will need to implement JavaScript.

Personally, I just set my character limit to about 1,000 or as much as I want the comment/message box to expand in height.

HTML

<div class="box">
<textarea name="" id="" maxlength="50"></textarea>
<div class="word-counter">0/100</div>
</div>
ARLCode
  • 651
  • 6
  • 20
2

You can cal a function like this:

function countwords(str){
  var count = 0;
  str=str.trim();
  for (var i = 0; i <= str.length; i++) {
     if (str.charAt(i) == " ") {
        count ++;
      }
  }
  return count;
}
Brijesh Bhatt
  • 3,810
  • 3
  • 18
  • 34
1

Try this

$('#text').keydown(function(e){
    var code=e.keyCode;
    var length=$(this).val().split(' ').length;
    if(length<=100)
    {
        $('.word-counter').text(length+'/100');
    }
    else
    {
        if(code!=37&&code!=38&&code!=39&&40&&code!=8&&code!=46)
        {
        return false;
        }
    }
});

Working Demo

Manoj
  • 4,951
  • 2
  • 30
  • 56
0
$('#text').keyup(function(e) {
    var length=parseInt($(this).val().length)+1;
     switch (e.keyCode) {
            case 8:  // Backspace
            length--;
             $('.word-counter').text(length+'/100');
             break;
            case 9:  // Tab
            case 13: // Enter
            case 37: // Left
            case 38: // Up
            case 39: // Right
            case 40: // Down
            break;

            default:
                 if(length<=100)
                {
                    $('.word-counter').text(length+'/100');
                }
                else
                {
                    return false;
                }
        }         
    });

Use keyup() for letters counting
this is its working demo Working Demo

Junaid Ahmed
  • 720
  • 1
  • 7
  • 15