CoderZone.org

Category: >> JavaScript >> Limit Text Area Characters Bookmark and Share

<< lastnext >>

Snippet Name: Limit Text Area Characters

Description: Limit the number of characters that can be entered into a text area. Works with any number of text areas on the same page.

Comment: (none)

Author: CoderZone
Language: JAVASCRIPT
Highlight Mode: JAVASCRIPT
Last Modified: February 27th, 2009

<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>
 
<textarea name="mytext" id="mytext" style="width:99%; height:100px;" value="" 
onkeyup="textCounter(this,'count_display',255);" 
onBlur="textCounter(this,'count_display',255);"
></textarea><br>
<span style="position: relative; left: 15;">
<table cellpadding="0" cellspacing="0" border="0" style="font-size:10px;"><tr><td>
Chars left:&nbsp;</td><td width="30" bgcolor="#ffffbb"><span id="count_display">255</span>
</td><td>&nbsp;(255 max)</td></tr></table>
</span>
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 9