Set max characters in a textarea

I have an ASP page with several textareas. I want to set a maximum of 255 characters for each textarea. How would I go about doing this since there doesn't seem to be a MaxLength property for the textarea?
susantriderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

xabiCommented:
The only thing you can do is to add "onchange" event to your textarea this way:

<form name="myform">
<texarea name="mytext" onchange="test()"></textarea>
</form>

and create a javascript function called test() that check the length of the contens:

<javascript language="javascript">
<!--
function check() {
 if (document.myform.mytext.length > 255) {
  alert("you can only type 255 chars")
  document.myform.mytext.value = document.myform.mytext.value.substring(0,255)
 }
}
//-->
</javascript>

xabi
0
susantriderAuthor Commented:
Thanks!
0
xabiCommented:
xabi changed their proposed answer to a comment
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

AemerginCommented:
Use a javascript function to check it, like this:

---------------------------------------
<SCRIPT LANGUAGE="javascript">
function textareacount(myTextArea, maxCount) {
  var overLimit
  if(myTextArea.value.length > maxCount) {
    overLimit = " - EXCEEDED"
  }
  else
  {
    overLimit = " "
  }
  window.status = myTextArea.name + ': ' + myTextArea.value.length + ' of ' + maxCount + overLimit ;
}

function checkText()
{
if (Message.value.length > 255)
  {
  alert("You have exceed the maximum number of allowed characters in the Message box. Please correct this.");
  return false;
  }
}
</SCRIPT>
---------------------------------------
Your textarea should be setup like:

<textarea name=Message cols=45 rows=5 onkeyup="textareacount(this, '255');" onmousemove="textareacount(this, '255');"></textarea>

Your form should be set like:

<form name=frmEmail action="submit.asp" method=post onSubmit="check()">
---------------------------------------

This will not only tell you how many characters are in the textarea while you type, it will also give you an alert box when you try to submit.

-Aemergin
0
xabiCommented:
Sorry, the correct example is this one:

<html>
<head>
<title>Example</title>
<script language="javascript">
<!--
function check() {
 if (document.myform.mytext.value.length > 255) {
  alert("you can only type 255 chars")
  document.myform.mytext.value = document.myform.mytext.value.substring(0,255)
  return false
 }
 return true
}
//-->
</script>
</head
<body>
<form name="myform">
<textarea name="mytext" onchange="check()"></textarea>
</form>
</body>
</html>

Sorry

xabi
0
xabiCommented:
That was the correct answer.

xabi
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AemerginCommented:
Oops .. looks like I posted while you two were working on this. ;)

-Aemergin
0
susantriderAuthor Commented:
Adjusted points to 10
0
susantriderAuthor Commented:
Just curious, how would this be done using VBScript instead of JavaScript?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.