Solved

Limit multiline text box, show number of characters left

Posted on 2008-10-29
4
2,352 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
Comment Utility
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
Comment Utility
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
Comment Utility
Can that be made work for a asp:TextBox?
0
 
LVL 12

Expert Comment

by:techExtreme
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now