Problem: To limit the number of characters entered in a textarea including copy paste issues.
HTML Textarea Limitations:
1) No maxlength property available like input tag.
2) Paste event only available in IE.
Cross browser solution: (Works on IE & Mozilla)
< textarea rows="10" cols="50"
onkeypress = "e = event || window.event; code = e.charCode || e.keyCode;
if((code == 8 || code == 46) || (code >= 37 && code <= 40))
return true; else return (this.value.length <= 100);"
onpaste =
"if((this.value.length +
window.clipboardData.getData('Text').length) > 100)
{ this.value +=
window.clipboardData.getData('Text').substr(0,
(100 - this.value.length));
alert('There is a limit of 100 characters in textarea.
Text beyond this limit has not been added.');
event.returnValue = false; }"
oninput = "if(this.value.length > 100){
this.value = this.value.substr(0,100);
alert('There is a limit of 100 characters in textarea.
Text beyond this limit has not been added.'); }">
< /textarea >
No comments:
Post a Comment