Skip to content

textarea – maxlength equivalent – easy way of limiting the max number of characters

2012 April 3

The easiest way to limit the maximum length someone can type into a textarea field of your form is to do the following:

<textarea name=”mytextarea” id=”mytextarea” cols=”50″ rows=”4″ onkeypress=”return (this.value.length < 20);” >

Where 20 is the maximum number of characters you want to show.  This seems to work well on Chrome and IE.  I found a lot of other examples on the web that require 30 lines of code, but this is the simplest way, just using the onkeypress event.

onkeypress=”return (this.value.length < 20);”

 

No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS