Last updated on February 14th, 2022 at 01:40 pm
Limit Textarea Characters Javascript
This tutorial will give you the idea of limiting the textarea characters inside a html form using simple Javascript. Here I am limiting the characters to 125. You can change it according to your convenience.
The script will look like this
<script language=javascript>
var count = "125"; //Example: var count = "175";
function limiter(){
var tex = document.myform.comment.value;
var len = tex.length;
if(len > count){
tex = tex.substring(0,count);
document.myform.comment.value =tex;
document.myform.limit.value = '';
return false;
}
document.myform.limit.value = 'Typing';
}
</script>
We do have a HTML form where we have the textarea displayed.
<form name="myform" METHOD=POST>
<textarea name=comment wrap=physical rows=3 cols=40 onkeyup=limiter()></textarea><br>
<script language=javascript>
document.write("<input type=text name=limit size=4 readonly value="+count+">");
</script>
</form>
Just copy the above 2 set of script in to an html page and see how it works.