Solved

Limit multiline text box, show number of characters left

Posted on 2008-10-29
4
2,355 Views
Last Modified: 2013-11-07
I am creating a webpage to allow users send a text message.  Ho do I limit the amount of characters in a multiline text box and how do I show how many characters spaces are left.
I am using VB.NET for this web project.
0
Comment
Question by:iepaul
  • 3
4 Comments
 
LVL 12

Expert Comment

by:techExtreme
ID: 22830032
Hello,
you have to use javascript

Here's the script code and a sample textarea object that you can place in your page

<script type="text/javascript">
//Max length multiline textbox checker
function checkMaxLen(txt,maxLen) {
try{
if(txt.value.length > (maxLen-1)) {
var cont = txt.value;
txt.value = cont.substring(0,(maxLen -1));
return false;
};
}catch(e){
}
};
</script>



the element:

<textarea onkeyup="return checkMaxLen(this,5000)"></textarea>


Source Url: http://blog.michaelfasani.com/2008/08/limiting-a-textbox-with-javascript

Hope it helped.
0
 
LVL 12

Accepted Solution

by:
techExtreme earned 500 total points
ID: 22830234
Hi, this is the modified version of the previous script that uses a span element to display number of characters left that can be entered.
For this to work you must put these both elements in your page, the text area and the span tag with id: count.
Just copy this and paste in a sample test html page and run , it should work fine.


<textarea onkeyup="return checkMaxLen(this,15)"></textarea>
<span id="count">Chars Left: 0</span>



<script type="text/javascript">
//Max length multiline textbox checker
function checkMaxLen(txt,maxLen)
{
 
  try
  {
     if(txt.value.length > (maxLen-1))
     {
         var cont = txt.value;
         txt.value = cont.substring(0,(maxLen -1));
     document.getElementById("count").innerHTML = 0;    
         return false;
     };
  }
  catch(e)
  {
  }
  document.getElementById("count").innerHTML = maxLen - txt.value.length ;
 
};
</script>


Hope it helped
0
 

Author Comment

by:iepaul
ID: 22830583
Can that be made work for a asp:TextBox?
0
 
LVL 12

Expert Comment

by:techExtreme
ID: 22832790
Yes ofcourse it can work with asp:textbox.
However its altogether a different game.

But this should work as of without any problem I think.
Let me know of any issues you face.

Happy Javascripting!

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

939 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