Take a look at this page:
Interactive character limit for textarea using Jquery
<script language="javascript">
function limitChars(textid, limit, infodiv)
{
var text = $('#'+textid).val();
var textlength = text.length;
if(textlength > limit)
{
$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
$('#'+textid).val(text.substr(0,limit));
return false;
}
else
{
$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
return true;
}
}
</script>
Then bind the function to the keyup event of your textarea. Do this in jQuery’s ready event of document like this:
$(function(){
$('#comment').keyup(function()
{
limitChars('comment', 20, 'charlimitinfo');
})
});