Maxlength text area not working

I want to set the maximum length in textarea. I use the following code for the same, but it does not work,

<textarea name="txtDescription" cols=10 rows=3 maxlength=50></textarea>

But it does not work, it accepts characters beyond 50.

+5
source share
9 answers

There is no attribute maxlengthdefined for textarea. You need to implement this using javascript .

+4
source

Yup maxlength does not work in textarea. But you can use jQuery something like this:

    var $limitNum = 500;
    $('textarea[name="textarea_name"]').keydown(function() {
        var $this = $(this);

        if ($this.val().length > $limitNum) {
            $this.val($this.val().substring(0, $limitNum));
        }
    });
+1
source

IE 10.

maxlength, . maxlength HTML5. IE 10+ Chrome.

0

maxlength textarea, ( EOL). max_length maxlength.

HTML:

<textarea name="whatever" max_length="100"></textarea>

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

<textarea name="testString" onkeypress="if (this.value.length >= 250) { return false; }"id="" cols="30" rows="10" maxlength="250"></textarea>
0

Maxlength textarea Chrome.

-1

HTML, , , "".

<textarea maxlength="5"></textarea>
Hide result
-2

Ruby on Rails:

:maxlength => "10"

:

<%= text_area 'order', 'special_instructions_display', :size => "85x7", :maxlength => "10"  %>
-2

jquery

<script type="text/javascript">  
$().ready(function(){   

     $("#textareaId").maxLength(20);   

 });
</script>  
-3

All Articles