Home > Style & Scripts > JavaScript > To count the characters in a textarea

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;


<!-- end of Script  -->



In the body section put the below code :


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



Hope that it is useful.

You may also like
How to convert feeds to html using javascript and php
Simple JavaScript validation for dates
Multi-language support for web application with MySql and PHP
PHP Headers for your Web Application

1 Response

  1. raj sudan

    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

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