Count characters in a textarea.

You just need the bolded text…

<html>
<HEAD>

<script type=”text/javascript”>
function textCounter(textarea, counterID, maxLen) {
cnt = document.getElementById(counterID);
if (textarea.value.length > maxLen)
{
textarea.value = textarea.value.substring(0,maxLen);
}
cnt.innerHTML = maxLen – textarea.value.length;
}
</script>

</HEAD>
<body>
<center>
<form action=”whatever.php”>

<textarea name=”text_area” cols=”50″ rows=”5″ onKeyUp=”textCounter(this,’count_display’,3500);” >
onBlur=”textCounter(this,’count_display’,3500);” >
</textarea>
<br>

<span id=”count_display”>3500</SPAN> characters remaining

<input type=”submit” value=”Submit”>
</form>
</center>
</body>
</html>

About these ads

One Response to “Count characters in a textarea.”

  1. hyder Says:

    Nice code . I have implemented a similar stuff using Jquery . You can find the tutorial here :

    http://youhack.me/2010/04/22/live-character-count-with-progress-bar-using-jquery/

    The difference with this one is that i have implemented a progress bar based on the number of characters entered .
    Enjoy !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: