<p>How to restrict the maximum number of characters that can be entered into an HTML <code>&lt;textarea&gt;</code>? I'm looking for a cross-browser solution.</p>

[{'answer_id': 43571, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 5, 'selected': True, 'text': '<p>The <code>TEXTAREA</code> tag does not have a <code>MAXLENGTH</code> attribute the way that an\n<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>\n\n<pre><code>&lt;textarea onKeyPress="return ( this.value.length &lt; 50 );"&gt;&lt;/textarea&gt;\n</code></pre>\n\n<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>\n\n<p>This works because the Boolean expression compares the field\'s length\nbefore 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.\nSo if the current length is already 50 (or more), the handler returns false,\nthe <code>KeyPress</code> action is cancelled, and the character is not added.</p>\n\n<p>One fly in the ointment is the possibility of pasting into a <code>TEXTAREA</code>,\nwhich does not cause the <code>KeyPress</code> event to fire, circumventing this check.\nInternet Explorer 5+ contains an <code>onPaste</code> event whose handler can contain the\ncheck. However, note that you must also take into account how many\ncharacters are waiting in the clipboard to know if the total is going to\ntake you over the limit or not. Fortunately, IE also contains a clipboard\nobject from the window object.<a href="http://lists.evolt.org/archive/Week-of-Mon-20040315/156773.html" rel="nofollow noreferrer">1</a> Thus:</p>\n\n<pre><code>&lt;textarea onKeyPress="return ( this.value.length &lt; 50 );"\nonPaste="return (( this.value.length +\nwindow.clipboardData.getData(\'Text\').length) &lt; 50 );"&gt;&lt;/textarea&gt;\n</code></pre>\n\n<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>\n\n<p><a href="http://lists.evolt.org/archive/Week-of-Mon-20040315/156773.html" rel="nofollow noreferrer">Source</a></p>\n\n<p>Also, there is another way here, including a finished script you could include in your page:</p>\n\n<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>\n'}, {'answer_id': 11957631, 'author': 'indusBull', 'author_id': 854656, 'author_profile': 'https://Stackoverflow.com/users/854656', 'pm_score': 3, 'selected': False, 'text': '<p>HTML5 now allows <a href="https://developer.mozilla.org/en/docs/Web/HTML/Element/textarea#attr-maxlength" rel="nofollow"><code>maxlength</code> attribute on <code>&lt;textarea&gt;</code></a>. </p>\n\n<p>It is supported by all browsers except IE &lt;= 9 and iOS Safari 8.4. See <a href="http://caniuse.com/#feat=maxlength" rel="nofollow">support table on caniuse.com</a>.</p>\n'}, {'answer_id': 23005615, 'author': 'ngrashia', 'author_id': 3492139, 'author_profile': 'https://Stackoverflow.com/users/3492139', 'pm_score': -1, 'selected': False, 'text': '<pre><code>$(function(){  \n  $("#id").keypress(function() {  \n    var maxlen = 100;\nif ($(this).val().length &gt; maxlen) {  \n  return false;\n}  \n})\n});  \n</code></pre>\n\n<p>Reference <a href="https://stackoverflow.com/questions/4459610/set-maxlength-in-html-textarea">Set maxlength in Html Textarea</a></p>\n'}]
['https://Stackoverflow.com/questions/43569', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3512/']