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!’);
}
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>
<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:



















