Solved

limit number of characters in <textarea>

Posted on 2001-06-24
7
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: 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!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
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 …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

626 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