I want to set maximum length to the textarea. I am using following code for the same,but it is not working,
I tried maxlength in textarea, but does not count the characters correctly(also counts the EOLs).
I use max_length
attribute instead of normal maxlength
.
HTML:
JS:
$('textarea').keyup(function(){
var maxlength = parseInt($(this).attr('max_length')),
text = $(this).val(),
eol = text.match(/(\r\n|\n|\r)/g),
count_eol = $.isArray(eol) ? eol.length : 0,//error if eol is null
count_chars = text.length - count_eol;
if (maxlength && count_chars > maxlength)
$(this).val(text.substring(0, maxlength + count_eol));
});