473,508 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to display how many left to type in the text box

254 Contributor
Hi

I want to display the number of character to type in a textbox when ever user types a character to the text box using javascript

How can I do that?
Feb 4 '08 #1
12 1637
acoder
16,027 Recognized Expert Moderator MVP
Assuming you have something like:
Expand|Select|Wrap|Line Numbers
  1. <input type="text" id="textBox" name="textBox" maxlength="50" onkeyup="charCount(this.maxLength);"><span id="charsLeft">50 characters left.</span>
code like this should do the trick:
Expand|Select|Wrap|Line Numbers
  1. function charCount(remaining) {
  2.   var textBox = document.getElementById("textBox");
  3.   var spanObj = document.getElementById("charsLeft");
  4.   var charsLeft = remaining - textBox.value.length;
  5.   if (charsLeft <= 0) {
  6.     charsLeft = 0;
  7.     textBox.value = textBox.value.substring(0,remaining);
  8.   }
  9.   spanObj.innerHTML = charsLeft + " characters left.";
  10. }
Feb 4 '08 #2
mukeshrasm
254 Contributor
Assuming you have something like:
Expand|Select|Wrap|Line Numbers
  1. <input type="text" id="textBox" name="textBox" maxlength="50" onkeyup="charCount(this.maxLength);"><span id="charsLeft">50 characters left.</span>
code like this should do the trick:
Expand|Select|Wrap|Line Numbers
  1. function charCount(remaining) {
  2.   var textBox = document.getElementById("textBox");
  3.   var spanObj = document.getElementById("charsLeft");
  4.   var charsLeft = remaining - textBox.value.length;
  5.   if (charsLeft <= 0) {
  6.     charsLeft = 0;
  7.     textBox.value = textBox.value.substring(0,remaining);
  8.   }
  9.   spanObj.innerHTML = charsLeft + " characters left.";
  10. }
Thanks

Actually I was doing in this way. This code is working but not as I wanted.
Expand|Select|Wrap|Line Numbers
  1. function left()
  2. {
  3.     var maxlen=20;
  4.     var len=document.che.firstname.value.length;
  5.     if(len>maxlen)
  6.     {
  7.         return false;
  8.         exit();
  9.     }
  10.     else
  11.     {
  12.         var str=document.che.firstname.value.length;
  13.         var str=maxlen-str;
  14.         document.write(str+"charaters left");
  15.     }
  16. }
Expand|Select|Wrap|Line Numbers
  1. <form name="che">
  2. <input type="text" value="" name="firstname"  onkeyup="left()" />
So now I came to know where I was wrong.
Feb 4 '08 #3
acoder
16,027 Recognized Expert Moderator MVP
Glad it helped fix it.

Don't use document.write after the page has loaded.

Remember to close your code tags with [/code] otherwise the code won't appear as code.
Feb 4 '08 #4
mukeshrasm
254 Contributor
Glad it helped fix it.

Don't use document.write after the page has loaded.

Remember to close your code tags with [/code] otherwise the code won't appear as code.
I have three boxes in which I wanted to use this feature so for rest textbox do I need to write the same code or it can be done in one go. if it is then how can I do it.
Feb 4 '08 #5
acoder
16,027 Recognized Expert Moderator MVP
I have three boxes in which I wanted to use this feature so for rest textbox do I need to write the same code or it can be done in one go. if it is then how can I do it.
You just need to adapt the code to work with all three. If you add an extra parameter to identify the textbox, it should work fine with all three. Post your HTML code if you get stuck.
Feb 4 '08 #6
mukeshrasm
254 Contributor
You just need to adapt the code to work with all three. If you add an extra parameter to identify the textbox, it should work fine with all three. Post your HTML code if you get stuck.
Here is the HTML code.

Expand|Select|Wrap|Line Numbers
  1. //html
  2. <input name="firstname"type="text" id="textBox"  value="" size="20" maxlength="20" /><span id="charsLeft" >20 Characters Left</span>
  3.  
  4. <input name="middlename"type="text" id="textBoxm" value="" size="20" maxlength="20" /><span id="charsLeftm" >20 Characters Left</span>
  5.  
  6. <input name="lastname"type="text" id="textBoxl"  value="" size="20" maxlength="20" /><span id="charsLeftl" >20 Characters Left</span>
  7.  
  8.  
Feb 5 '08 #7
acoder
16,027 Recognized Expert Moderator MVP
The only changes you need to make are in the first three lines of the function:
Expand|Select|Wrap|Line Numbers
  1. function charCount(remaining, id) {
  2.   var textBox = document.getElementById("textBox"+id);
  3.   var spanObj = document.getElementById("charsLeft"+id);
and in the HTML:
Expand|Select|Wrap|Line Numbers
  1. <input name="firstname" type="text" id="textBox" value="" size="20" maxlength="20" onkeyup="charCount(this.maxLength,'')" /><span id="charsLeft" >20 Characters Left</span>
  2.  
  3. <input name="middlename" type="text" id="textBoxm" value="" size="20" maxlength="20" onkeyup="charCount(this.maxLength,'m')" /><span id="charsLeftm" >20 Characters Left</span>
  4.  
  5. <input name="lastname" type="text" id="textBoxl"  value="" size="20" maxlength="20" onkeyup="charCount(this.maxLength,'l')" /><span id="charsLeftl" >20 Characters Left</span>
Feb 5 '08 #8
mukeshrasm
254 Contributor
The only changes you need to make are in the first three lines of the function:
Expand|Select|Wrap|Line Numbers
  1. function charCount(remaining, id) {
  2.   var textBox = document.getElementById("textBox"+id);
  3.   var spanObj = document.getElementById("charsLeft"+id);
and in the HTML:
Expand|Select|Wrap|Line Numbers
  1. <input name="firstname" type="text" id="textBox" value="" size="20" maxlength="20" onkeyup="charCount(this.maxLength,'')" /><span id="charsLeft" >20 Characters Left</span>
  2.  
  3. <input name="middlename" type="text" id="textBoxm" value="" size="20" maxlength="20" onkeyup="charCount(this.maxLength,'m')" /><span id="charsLeftm" >20 Characters Left</span>
  4.  
  5. <input name="lastname" type="text" id="textBoxl"  value="" size="20" maxlength="20" onkeyup="charCount(this.maxLength,'l')" /><span id="charsLeftl" >20 Characters Left</span>
Thanks

It workes fine as I wanted to do.
Feb 5 '08 #9
acoder
16,027 Recognized Expert Moderator MVP
You're welcome. Glad to hear that it worked as you wanted.
Feb 5 '08 #10
mukeshrasm
254 Contributor
You're welcome. Glad to hear that it worked as you wanted.
Hi
I got one more problem but it is with php and gd library it is header allready sent if you want I will show you my code so please reply me back.
Feb 6 '08 #11
acoder
16,027 Recognized Expert Moderator MVP
Since this is not related to this thread, start a new thread in the correct forum (it's a PHP question?) if you haven't already done so.
Feb 6 '08 #12
mukeshrasm
254 Contributor
Since this is not related to this thread, start a new thread in the correct forum (it's a PHP question?) if you haven't already done so.

Ok. I have posted it in PHP forum with Title: Cannot modify header information - headers already sent. So please suggest me what to do?

Thanks!
Feb 6 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2181
by: Scott | last post by:
Relative newbie here, I'm looking to display the value of radio buttons and check boxes on the page before submission. So far I can do most of it. When "Hat" is checked there are to be no color...
2
3338
by: ruby_bestcoder | last post by:
Hi Im having problem in firefox with display:none/block. I have essentially 3 li elements. In each element there are a few nested div:s. Clicking on one of the divs, makes another div to...
3
2074
by: Paul | last post by:
I found a great site for CSS tempaltes, http://blog.html.it/layoutgala/ . I downloaded them, and played around with them. Then I tried to use the template in as ASP.Net MasterPage, but everything...
5
1961
by: CES | last post by:
All, I was hoping that someone might be able to help me with a few questions on Aligning Block Elements properly... Basically I have a row that has a fixed width of 900px. Within the row their is...
1
4511
by: rbinington | last post by:
Hi, I am trying to write a DNN module that has the ability to insert articles into an article repository. I want the users to be able to move pages around and enter text into the FCKEditor. I...
4
9022
ak1dnar
by: ak1dnar | last post by:
Hi, I have created a PHP page that write down form data to mySQL table. Before i submit data I want to check the Input field, whether it has filled up or Not. the form should validate from the PHP...
15
3127
by: cssExp | last post by:
hello, Rather than going on a wild explanation on what's the the problem, it'll be much quicker and easier if i let you look at it yourself, so I'll post my page source (actual contents taken out,...
2
3229
by: wreed06 | last post by:
Hello, I have 2 problems. In my webpage, I have a dropdown list with a button that takes the user to a popup window specific to the option. I am using Firefox 2.0.0.13. I have successfully...
14
4337
by: confusedfusion | last post by:
Not sure how many form submissions that have been lost over the years before I started but the company has a contact form that the required fields when validation fails the error message is going...
1
2611
by: jeddiki | last post by:
Hello, I have made a nice opt-in form and tested it in Moz FF and it looks fine. But in IE the elements don't line up properly. I think I am nearly there but can not get these elements...
0
7133
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5643
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4724
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1568
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
435
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.