View Full Version : Max size of characters allowed to type in textarea

05-06-2008, 02:04 AM
For a textarea how do I limit the number of characters in the box to 500?

For input text I can set to maxlength and limit the entry in the input: <input type="text" maxlength="500" name="myfield">
For textarea I assume I need to create a validation or something to fetch the length of the field and stop the entry after 500 characters. Please advise how this can be done.

05-06-2008, 04:43 AM
A quick Google (http://www.google.com/search?q=limit+characters+in+textfield&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official) search turned up this: http://www.mediacollege.com/internet/javascript/form/limit-characters.html.

05-07-2008, 03:22 AM
Thanks it works but I also want a alert message and cant seem to get it to work because not alert message appears once the 500 characters is entered.
Please advise:

<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
alert("You are trying to enter more than 500 characters");
} else {
limitCount.value = limitNum - limitField.value.length;

<form name="myform">
<textarea name="limitedtextarea" onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown,500);"

Philip M
05-07-2008, 09:03 AM
Over-complicated. Try this:-

<script type="text/javascript">

function limitText(limitField) {
fldValue = limitField.value;
var limitNum = 500;
var chars = limitNum - fldValue.length;
alert (chars); // delete after testing
if (chars <= 0) {
alert ("You are trying to enter more than the limit of " + limitNum + " characters! ");
fldValue = fldValue.substring(0,limitNum-1)
document.myform.limitedtextarea.value = fldValue;
if (chars == 20) {
alert ("You are approaching the limit of " + limitNum + " characters and have only 20 characters left! ")


<form name="myform">
<textarea name="limitedtextarea" onKeyDown="limitText(limitedtextarea)";