How to Limit the Number of Characters in a Textarea witch Character Counter

I found a good javascript to limit the character in a textarea and it is very cool because it has a character counter.

Here is the code:
put this between your <head>…. </head>:

<script language=”javascript”>
function limit(what,chars,counter) {
if (what.value.length > chars) {
what.value=what.value.substr(0,chars);
alert(‘You exceed to ‘ + chars + ‘chars!’);
}

counting = (chars – what.value.length);
c = document.getElementById(counter);
c.innerHTML = counting;
}
</script>

put this in you <body>:

<p><label for=”text”><strong>Text</strong></label> ¦ Chars left: <span id=”count1″>500</span></p>
<textarea name=”[1][1][t]” rows=”10″ cols=”50″ onkeyup=”limit(this,500,’count1′);” onkeydown=”limit(this,500,’count1′);” onblur=”limit(this,500,’count1′);” onfocus=”limit(this,500,’count1′);”></textarea>

The code above will limit the textarea in 500 character change it base on what you need.

Related Topics:

The JavaScript Source: Forms: Limit Textarea

Character Counter/Limiter Java Script

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Furl
  • Reddit
  • Smarking
  • StumbleUpon
  • Technorati
  • TwitThis
  • YahooMyWeb
  • Simpy
  • Propeller
  • Fark
  • LinkedIn
  • Live
  • Mixx

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment