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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!-- 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 > 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 :
************************************
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <!-- 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 --> |
**************************************
Hope that it is useful.
1 Comment
raj sudan · August 14, 2014 at 6:17 am
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..