Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

limit number of characters in <textarea>

Posted on 2001-06-24
7
Medium Priority
?
282 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 120 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
Technology Partners: 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Find out what you should include to make the best professional email signature for your organization.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 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