views:

14363

answers:

3

I would like to have some functionality by which if i write

<textarea maxlength="50"></textarea>
<textarea maxlength="150"></textarea>
<textarea maxlength="250"></textarea>

it will automatically impose the maxlength on the textArea. If possible please donot provide the solution in JQuery.

Note: This can be done if i do something like this:

<textarea onkeypress="return imposeMaxLength(event, this, 110);" rows="4" cols="50">


function imposeMaxLength(Event, Object, MaxLen)
{
        return (Object.value.length <= MaxLen)||(Event.keyCode == 8 ||Event.keyCode==46||(Event.keyCode>=35&&Event.keyCode<=40))
}

copied from another thread

But the point is I don't want to write onKeyPress and onKeyUp every time i declare a textArea.

+19  A: 
window.onload = function() { 
  var txts = document.getElementsByTagName('TEXTAREA') 

  for(var i = 0, l = txts.length; i < l; i++) {
    if(/^[0-9]+$/.test(txts[i].getAttribute("maxlength"))) { 
      var func = function() { 
        var len = parseInt(this.getAttribute("maxlength"), 10); 

        if(this.value.length > len) { 
          alert('Maximum length exceeded: ' + len); 
          this.value = this.value.substr(0, len); 
          return false; 
        } 
      }

      txts[i].onkeyup = func;
      txts[i].onblur = func;
    } 
  } 
}
Josh Stodola
Josh it seems it will work but will u please explain what this thing will do ---if(/^[0-9]+$/.test(txts[i].getAttribute("maxlength"))) {---
Rakesh Juyal
That makes sure the maxlength attribute is numeric before assigning the event handler.
Josh Stodola
I bet this has some problems when the textarea has hard line breaks in it (that is, line breaks explicitly inserted by the user by hitting "return"). Firefox and IE report the text length differently when there are hard line breaks. (At least, I think I remember that.)
Pointy
I think I remember what the deal is: either FF or IE (I think it's FF) returns a different string when Javascript checks the "value" attribute than what it sends back to the server when the form is posted! It has something to do with how hard line breaks do/don't get a carriage return character inserted. It's easy to figure out with some debug code on the client and server sides.
Pointy
Answer updated - changed to additionally handle the `onblur` event to enforce the maxlength when the user tries to paste with the context menu.
Josh Stodola
I switched the order of the alert and the value truncate - in the original order, the onkeyup would alert, causing the control to lose focus and the onblur to fire since the field had not been truncated yet.
GalacticCowboy
+1  A: 

Also add the following event to deal with pasting into the textarea:

...

txts[i].onkeyup = function() {
  ...
}

txts[i].paste = function() {
  var len = parseInt(this.getAttribute("maxlength"), 10);

  if (this.value.length + window.clipboardData.getData("Text").length > len) {
    alert('Maximum length exceeded: ' + len);
    this.value = this.value.substr(0, len);
    return false;
  }
}

...
Stuart
Could someone add this to the answer body if they think it's ok? Don't have enough points to do that yet.
Stuart
@Stuart The paste function is not standardized. I believe it only works in IE.
Josh Stodola
@Stuart I updated my answer to handle the paste situation. Thank you!
Josh Stodola
A: 

instead of code from skodola , you can copy the part of the code from jquery that binds the length check . Exhaustive test have been done on cross browser.

Suresh S