[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Set max characters in a textarea

Posted on 2000-01-25
9
Medium Priority
?
555 Views
Last Modified: 2012-08-13
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?
0
Comment
Question by:susantrider
  • 4
  • 3
  • 2
9 Comments
 
LVL 8

Expert Comment

by:xabi
ID: 2385709
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
 

Author Comment

by:susantrider
ID: 2385720
Thanks!
0
 
LVL 8

Expert Comment

by:xabi
ID: 2385726
xabi changed their proposed answer to a comment
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 1

Expert Comment

by:Aemergin
ID: 2385727
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
 
LVL 8

Expert Comment

by:xabi
ID: 2385728
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
 
LVL 8

Accepted Solution

by:
xabi earned 40 total points
ID: 2385729
That was the correct answer.

xabi
0
 
LVL 1

Expert Comment

by:Aemergin
ID: 2385730
Oops .. looks like I posted while you two were working on this. ;)

-Aemergin
0
 

Author Comment

by:susantrider
ID: 2393371
Adjusted points to 10
0
 

Author Comment

by:susantrider
ID: 2394223
Just curious, how would this be done using VBScript instead of JavaScript?
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Suggested Courses
Course of the Month9 days, 5 hours left to enroll

590 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