Solved

Limit multiline text box, show number of characters left

Posted on 2008-10-29
4
2,371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…

710 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