Text Area maxlength not working

后端 未结 10 1906
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 01:28

I want to set maximum length to the textarea. I am using following code for the same,but it is not working,


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));
});

提交回复
热议问题