Hulk
Hulk

Reputation: 34200

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.....

Upvotes: 3

Views: 34690

Answers (8)

Marcus L
Marcus L

Reputation: 4078

One Google away.

<script language="javascript" type="text/javascript">
<!--
function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}
-->
</script>

Implementation:
<textarea name="myName" onkeypress="return imposeMaxLength(this, 50);" ></textarea> 

EDIT:

Code that doesn't freeze text:

<script type="text/javascript">

/***********************************************
* Textarea Maxlength script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function imposeMaxLength(obj) {
        const mlength = obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : ""
        if (obj.getAttribute && obj.value.length > mlength) {
            obj.value = obj.value.substring(0, mlength)
        }
        obj.value = obj.value.trim()
    }

</script>

<textarea maxlength="40" onkeyup="return imposeMaxLength(this)"></textarea>

Upvotes: 10

detale
detale

Reputation: 12930

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

Upvotes: 0

Fawad Ghafoor
Fawad Ghafoor

Reputation: 6217

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>

Upvotes: 0

Surinder
Surinder

Reputation: 106

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

Upvotes: 3

Dss
Dss

Reputation: 2370

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

Upvotes: 6

zaf
zaf

Reputation: 23274

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

Upvotes: 15

nik
nik

Reputation: 3678

<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>

Upvotes: 2

Dror
Dror

Reputation: 7303

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

Upvotes: 2

Related Questions