'Textarea maxlength check

Textarea validation,

How to limit the characters in a textarea for not exceeding more than 50 characters.

<textarea rows="5" cols="15"></textarea>

Thanks.....



Solution 1:[1]

Using:

<textarea rows="5" cols="15" maxlength="50"></textarea>

From http://sigswitch.com/2008/07/textarea-maxlength-with-jquery/:

  $(document).ready(function(){
     $('textarea[maxlength]').keyup(function(){
      var max = parseInt($(this).attr(’maxlength’));
      if($(this).val().length > max){
       $(this).val($(this).val().substr(0, $(this).attr('maxlength')));
      }

      $(this).parent().find('.charsRemaining').html('You have ' + (max - $(this).val().length) + ' characters remaining');
     });
    });

Solution 2:[2]

Much simpler inline method:

<textarea cols="60" rows="5" onkeypress="if (this.value.length > 100) { return false; }"></textarea>

Change the "100" to however many characters you want

Solution 3:[3]

<asp:TextBox ID="txtColumn2" runat="server" TextMode="MultiLine" MaxLength="500" onkeyDown="checkTextAreaMaxLength(this,event,'500');"
onblur="onBlurTextCounter(this,'500');"></asp:TextBox>


function checkTextAreaMaxLength(textBox, e, maxLength) {
    if (!checkSpecialKeys(e)) {
        if (textBox.value.length > maxLength - 1) {
            if (window.event)//IE
                e.returnValue = false;
            else//Firefox
                e.preventDefault();
        }
    }
    onBlurTextCounter(textBox, maxLength);
}

function checkSpecialKeys(e) {
    if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40)
        return false;
    else
        return true;
}

function onBlurTextCounter(textBox, maxLength) {
    if (textBox.value.length > maxLength)
        textBox.value = textBox.value.substr(0, maxLength);
}

Solution 4:[4]

I don't know of a built in HTML way but you can use this:

<textarea rows="5" cols="15" onkeydown="return validateCharLimit(this);"></textarea>??????????????

function validateCharLimit(area) 
{
    var limit = 50;
    var iChars = area.value.length;
    if (iChars > limit) {
        return false;
    }
    return true;
}

Solution 5:[5]

<script>
function chkLen()
{
var tlen=document.getElementById('myTA').value.length;
if(tlen>50)
{
document.getElementById('myTA').value.substr(0,49)
}
}
</script>

<body>
    <textarea id="myTA" onkeyup='chkLen()'></textarea>
</body>

Solution 6:[6]

Limit Number of Characters in a TextArea using jQuery

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
   <title>Limit Number of Characters in a TextArea</title>
    <script type='text/javascript' src='http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js'></script>
  <script type='text/javascript'>
    $(document).ready(function() {
        $('#ta').keyup(function() {
            var len = this.value.length;
            if (len >= 50) {
                this.value = this.value.substring(0, 50);
            }
            $('#charLeft').text(50 - len);
        });
    });
  </script>
</head>
<body>
<textarea id="ta" cols="15" rows="5"></textarea><br/>
  (Maximum characters: 50)<br/>
  <span id="charLeft"> </span>  Characters left
</body>

Solution 7:[7]

Here's a solution utilizaing HTML 5 textarea's "maxLength" attribute and jQuery 1.7+.

1. Add "maxLength" attribute to textarea

<textarea cols="35" rows="3" maxLength="255" id="myTextArea" name="myTextArea">
</textarea>

2. Add jQuery event handler for the textarea

$(function() {
    $('#myTextArea').on('input propertychange', function () {
        var propMaxLength = $(this).prop('maxLength');
        if (!propMaxLength || typeof propMaxLength != 'number') {
            var maxLength = $(this).attr('maxlength'), txt = $(this).val();
            if (txt.length > maxLength) {
                $(this).val(txt.substr(0, maxLength));
            }
        }
    });
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dss
Solution 3 Surinder
Solution 4 Dror
Solution 5 nik
Solution 6 Fawad Ghafoor
Solution 7