JSFiddle - React, Tailwind, and code Playground
HTML
Max length 4.<br>
<textarea class="max"></textarea>
CSS
textarea{width:200px;height:200px;}
JavaScript
jQuery(document).ready(function($) {
var max = 4;
$('textarea.max').keypress(function(e) {
if (e.which < 0x20) {
// e.which < 0x20, then it's not a printable character
// e.which === 0 - Not a character
return; // Do nothing
}
if (this.value.length == max) {
e.preventDefault();
} else if (this.value.length > max) {
// Maximum exceeded
this.value = this.value.substring(0, max);
}
});
}); //end if ready(fn)