Solved

limit number of characters in <textarea>

Posted on 2001-06-24
7
243 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:shlomof
Comment Utility
ClassyLinks,

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

Author Comment

by:shlomof
Comment Utility
thanks dij8!!!
0
 
LVL 10

Expert Comment

by:dij8
Comment Utility
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
Comment Utility
Oh...is that what I did???  ;-)  LOL

Ciao for now!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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 …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now