Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Limit multiline text box, show number of characters left

Posted on 2008-10-29
4
Medium Priority
?
2,388 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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

661 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