How do I limit the number of characters in a textarea?
Which is the easiest way to limit the number of characters allowed to type in a textarea input field?
My textarea input field looks like this:
Type text here. (limit 150 characters)
Is there an html attribute that will do it, or do I use javascript?
Thanks,
Jakob
Status:
Open Sep 16, 2006 - 03:42 PM
html, Web Design, JavaScript, Text Areas
4answers
Answers
Sep 16, 2006 - 04:55 PM
There is no html attribute for that, you'll have to use javascript to do it.
You could have a function that would be called on each keypress and then deleting the character pressed if the maximum length was reached.
Sep 16, 2006 - 05:23 PM
Ok, thanks.
Something like this?:
150) this.value = this.value.substring(0, 150)">
Jakob
Sep 16, 2006 - 05:29 PM
yes, I believe that should work
Sep 16, 2006 - 05:33 PM
It works, though you can cheat it by copy/pasting some content to the field.
(Of course you'll have to use the right-click menu and NOT the CTRL + V shortcut...)
Add the onchange attribute with the same functionality to avoid this exploit.
Answer this question
Share Your Own Experience & Expertise
We look to ensure that every question is answered by the best people with relevant expertise and experience, the best answers include multiple perspectives. Do you have relevant expertise or experience to contribute your answer to any of these commonly asked questions?
Add New Comment