Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Limit multiline text box, show number of characters left

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

916 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