To count the characters in a textarea

Hi frineds ,

This is a simple script but we are using sometimes. To count the characters in a textarea using javascript explained with demo.

 

 

Javascript code

In the header section of the page:

<!-- Start the Script  -->
<script language=javascript>
//Edit the counter/limiter value as your wish
var count = "125";   //Example: var count = "143";
function limiter(){
var tex = document.myform.comment.value;
var len = tex.length;
if(len &gt; count){
tex = tex.substring(0,count);
document.myform.comment.value =tex;
return false;
}
document.myform.limit.value = count-len;
}

</script>

<!-- end of Script  -->

************************************

HTML Code

In the body section put the below code :

************************************

<!-- Script  -->
<form name="myform" METHOD="POST">
<table  style="border: #CCFF99 5px solid">
<tr>
<td>
<textarea name=comment wrap=physical rows=3 cols=40 onkeyup=limiter() style="border:#CCFF66 1px solid"></textarea></td>
</tr>
<tr><td align="right">Character left:
<script language=javascript>
document.write("<input type=text name=limit size=4 readonly value="+count+">");
</script>
</td></tr></table>
</form>
<!-- Script  -->

**************************************

counter

Hope that it is useful.

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ... Contact : [email protected]

You may also like...

1 Response

  1. raj sudan says:

    it’s not working for me.firstly it was showing error in if(len >count) then i remove semicolon then error disappared but when i m running this, it’s not working for me..

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.