0

In my js file inside a function, i m preparing like this

var x = '<span class="someclass">
         <textarea class="editField" maxlength="60">
         </textarea>      
         </span>';

here i m restricting the textarea size by using maxlength attribute, it is not working in IE.

Ganesh Rengarajan
  • 2,006
  • 13
  • 26
santhosh
  • 53
  • 1
  • 2
  • 11

5 Answers5

1

maxlength is not supported on <textarea> in all browsers. In any case, I don't think it's ever a good idea to actually restrain users from typing what they want. Tell them that they're over the limit, but don't lock them out.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • actually, it is supported in some browser, check out here: http://www.w3schools.com/tags/att_textarea_maxlength.asp note that only IE10 support it, IE9- (and older) will not – Jim Jul 29 '13 at 07:33
  • 1
    http://w3fools.com/, for one, but mainly I said "not [...] in all browsers", which means "yes in some browsers" – Niet the Dark Absol Jul 29 '13 at 07:39
  • thanks Kolink and jim.but in my requirement i need to restrict the user.I added this onKeyPress="return (this.value.length < 60);" along with maxlength attribute.Now maxlength is working but there will be a problem if user copy paste the text – santhosh Jul 30 '13 at 08:54
0

it wouldn't. IE10 now supports the HTML5 maxlength on a textarea. see here if you need to restrict length in previous IE versions then you'll need javascript for it.

Josh Meiburg
  • 201
  • 2
  • 8
0

Maxlength is not supported in IE, If you want to restrict your users from entering unlimited characters in IE, you would need to use javascript for that.

You could use the below script to limit users from entering maximum characters in textarea forIE.

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

And Your textarea can be like:

<textarea maxlength="10"></textarea>

DEMO

defau1t
  • 10,593
  • 2
  • 35
  • 47
0

The maxlength attribute is not standard for in HTML 4.01. It is defined in HTML5 though but I guess IE doesn't implement it. To make it work across all browsers you could use javascript. Here's an example.please check this it may helps you..thank you

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;
  } 
 } 
}
Manju
  • 747
  • 4
  • 10
  • 21
0

It's actually useless to restrain the user to type on his own end. This won't make any difference unless you don't test the length of what reaches the server (you really should).

I second what Kolink said, don't try to restrain the user from typing, but you can tell him that its input won't be accepted because it's exceeding.

Frederik.L
  • 5,522
  • 2
  • 29
  • 41