Limit textarea length using javascript

This is a simple code, but useful.

First, the javascript function:

function limit(n,id){
        // n is the number of max characters
        // id is the id of the textarea element
	text = document.getElementById(id);
	if(text.value.length >= n){
		//alert("sorry, the text is too long");
		text.value = text.value.substring(0, n-1);
	}
}

Then, the html implementation:

<textarea id="textoalimitar"
onkeypress="limit(10,'textoalimitar');" cols="45" rows="5"
 name="textoalimitar"></textarea>
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
Leave a comment

0 Comments.

Leave a Reply


[ Ctrl + Enter ]