Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>TEXTAREA</code> tag does not have a <code>MAXLENGTH</code> attribute the way that an <code>INPUT</code> tag does, at least not in most standard browsers. A very simple and effective way to limit the number of characters that can be <em>typed</em> into a <code>TEXTAREA</code> tag is:</p> <pre><code>&lt;textarea onKeyPress="return ( this.value.length &lt; 50 );"&gt;&lt;/textarea&gt; </code></pre> <p><strong>Note:</strong> <code>onKeyPress</code>, is going to prevent any button press, <strong>any button</strong> <em>including</em> the backspace key.</p> <p>This works because the Boolean expression compares the field's length before the new character is added to the maximum length you want (50 in this example, use your own here), and returns true if there is room for one more, <code>false</code> if not. Returning false from most events cancels the default action. So if the current length is already 50 (or more), the handler returns false, the <code>KeyPress</code> action is cancelled, and the character is not added.</p> <p>One fly in the ointment is the possibility of pasting into a <code>TEXTAREA</code>, which does not cause the <code>KeyPress</code> event to fire, circumventing this check. Internet Explorer 5+ contains an <code>onPaste</code> event whose handler can contain the check. However, note that you must also take into account how many characters are waiting in the clipboard to know if the total is going to take you over the limit or not. Fortunately, IE also contains a clipboard object from the window object.<a href="http://lists.evolt.org/archive/Week-of-Mon-20040315/156773.html" rel="nofollow noreferrer">1</a> Thus:</p> <pre><code>&lt;textarea onKeyPress="return ( this.value.length &lt; 50 );" onPaste="return (( this.value.length + window.clipboardData.getData('Text').length) &lt; 50 );"&gt;&lt;/textarea&gt; </code></pre> <p>Again, the <code>onPaste</code> event and <code>clipboardData</code> object are IE 5+ only. For a cross-browser solution, you will just have to use an <code>OnChange</code> or <code>OnBlur</code> handler to check the length, and handle it however you want (truncate the value silently, notify the user, etc.). Unfortunately, this doesn't catch the error as it's happening, only when the user attempts to leave the field, which is not quite as friendly.</p> <p><a href="http://lists.evolt.org/archive/Week-of-Mon-20040315/156773.html" rel="nofollow noreferrer">Source</a></p> <p>Also, there is another way here, including a finished script you could include in your page:</p> <p><a href="http://cf-bill.blogspot.com/2005/05/textarea-maxlength-revisited.html" rel="nofollow noreferrer">http://cf-bill.blogspot.com/2005/05/textarea-maxlength-revisited.html</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload