Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

JavaScript - Show Number of Characters Remaining in Textbox or Textarea

Feb 28, 2013
Introduction

Here I will explain how to show number of characters remaining in textbox or textarea using JavaScript

Description:
  
In previous articles I explained JavaScript restrict user to enter only numbers, JavaScript allow limited characters in textarea, JavaScript remove special characters from textbox JavaScript validations for phone, email and many articles relating to
JavaScript, jQuery, asp.net. Now I will explain how to show number of characters remaining in textbox or textarea using JavaScript.

To implement this we need to write the code like as shown below


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Limit the number of characters in textbox or textarea</title>
<script type="text/javascript">
function LimtCharacters(txtMsg, CharLength, indicator) {
chars = txtMsg.value.length;
document.getElementById(indicator).innerHTML = CharLength - chars;
if (chars > CharLength) {
txtMsg.value = txtMsg.value.substring(0, CharLength);
}
}
</script>
</head>
<body>
<div style="font-family:Verdana; font-size:13px">
Number of Characters Left:
<label id="lblcount" style="background-color:#E2EEF1;color:Red;font-weight:bold;">140</label><br/>
<textarea id="mytextbox" rows="5" cols="25" onkeyup="LimtCharacters(this,140,'lblcount');"></textarea>
</div>
</body>
</html>
Live Demo

For live demo check below example


Number of Characters Left:

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

15 comments :

Viral Story Insight said...

thanks its good

Viral Story Insight said...

thanks its good

sravan said...

It was not working while using master page

vijay said...

Thanks a lot dude ...
@ sravan: it's working for me with master page

Anonymous said...

hi sir

Unknown said...

hi sir , u solve my question.how count length of character in editor control using asp.net

Unknown said...

sir give me solution immediatly..

Anonymous said...

Doesn't work if you use right click and paste value in the textbox...

Anonymous said...

Thanks For The Code.
It is really helpful.

rajesh said...

good example but when i right click paste data then it showing that exceded text also

Anonymous said...

Apply the the script on onblur="LimtCharacters(this,140,'lblcount');" to make it more better.

Vaiyapuri Periyasamy said...

Thanks.

anudeep said...

How to limit the number of words to display in browser.For example we have a paragraph of two lines we can display it on large screens but in small screen we want to display only one line only so what should we do to hide remaining lines.

Peggy Gunn Milligan said...

Thank you! I know this is a couple of years old, but it is the first solution that I have found that works for me on an aspx page when I changed the textarea to a textbox. I gather that data on the server side to save to the database so didn't want to change the control I was using. With this being a content page and boxes needed to monitor on the main page and multiple pop-ups I was pulling my hair out. This did it. Thank you so much!

Kuch Jano said...

Thank you suresh..!!

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.