html - Maxlength attribute on textarea not working for mobile -
how restrict user not enter more maxlength in text area mobile. have tried maxlength attribute not working when user select text predective text.
i want limit text in text area 300 in case of mobile responsive website.
i using chrome , firefox browsers on mobile
the maxlength
attribute not stable on mobile browsers. looking @ mdn documentation attribute, find this
feature android firefox mobile (gecko) ie mobile opera mobile maxlength ? 4.0(2.0) no support ?
because of volability, recommend using simple javascript. this
var textfield = document.getelementbyid("text-field"); var charcount = document.getelementbyid("char-count"); textfield.addeventlistener("keydown", function(event) { if (this.value.length === 299 && event.keycode != 8) { event.preventdefault(); return false; } var length = event.keycode != 8 ? this.value.length + 1 : this.value.length - 1; charcount.innerhtml = (length) + " / " + "300"; charcount.style.color = length >= 270 ? "red" : "black"; });
<textarea id="text-field" rows="6" cols="30"></textarea> <p id="char-count"></p>
https://developer.mozilla.org/en-us/docs/web/html/element/textarea
Comments
Post a Comment