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.
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
|
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. |
|||
|
|||
15 comments :
thanks its good
thanks its good
It was not working while using master page
Thanks a lot dude ...
@ sravan: it's working for me with master page
hi sir
hi sir , u solve my question.how count length of character in editor control using asp.net
sir give me solution immediatly..
Doesn't work if you use right click and paste value in the textbox...
Thanks For The Code.
It is really helpful.
good example but when i right click paste data then it showing that exceded text also
Apply the the script on onblur="LimtCharacters(this,140,'lblcount');" to make it more better.
Thanks.
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.
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!
Thank you suresh..!!
Note: Only a member of this blog may post a comment.