Solved

limit number of characters in <textarea>

Posted on 2001-06-24
7
261 Views
Last Modified: 2013-12-03
Is there any way to limit number of character in <textarea> same as "maxlength" property in <input type="text" maxlength="50">?  I need something to work exectly same way - no warnings and no alerts.  Just prevent from additing more characters.
0
Comment
Question by:shlomof
  • 3
  • 2
  • 2
7 Comments
 
LVL 10

Expert Comment

by:dij8
ID: 6222644
Not really.  You could write a script that runs on keypress.  It would have to check the length of the value of the text area and if it is longer than a specific limit it would then change the value to the first however many characters.

<script language="javascript" type="text/javascript">
  function settalength(field) {
    field.value = field.value.substr(0,50)
  }
</script>
<textarea onkeypress="settalength(this)"></textarea>

Doing it this way will have the last character change as a new key is pressed.  And note that hitting return is good for two characters (carriage return, line feed).
0
 
LVL 7

Expert Comment

by:ClassyLinks
ID: 6222705
Here's a script that does limit the number of characters you can enter into a text box.  It's a little longer than dij8's, but you can see a working demo of it too.

http://www.shiningstar.net/articles/articles/javascript/dynamictextareacounter.asp?ID=AW
0
 
LVL 10

Accepted Solution

by:
dij8 earned 30 total points
ID: 6222727
The difference between these is the keypress bit.  To stop the last character changing like I did first change my script like so:
<textare onkeydown="settalength(this)" onkeyup="settalength(this)"></textarea>
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:shlomof
ID: 6222744
ClassyLinks,

This script is doing exactly the same as dij8's.  Thanks anyway.
0
 

Author Comment

by:shlomof
ID: 6222753
thanks dij8!!!
0
 
LVL 10

Expert Comment

by:dij8
ID: 6222877
Glad to have helped.  Thanks for the A.

And Classylinks, thanks for the subtle tip on fixing the last character problem I had. :-)
0
 
LVL 7

Expert Comment

by:ClassyLinks
ID: 6222937
Oh...is that what I did???  ;-)  LOL

Ciao for now!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question