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?
12 1637 acoder 16,027
Recognized Expert Moderator MVP
Assuming you have something like: - <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: - function charCount(remaining) {
-
var textBox = document.getElementById("textBox");
-
var spanObj = document.getElementById("charsLeft");
-
var charsLeft = remaining - textBox.value.length;
-
if (charsLeft <= 0) {
-
charsLeft = 0;
-
textBox.value = textBox.value.substring(0,remaining);
-
}
-
spanObj.innerHTML = charsLeft + " characters left.";
-
}
Assuming you have something like: - <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: - function charCount(remaining) {
-
var textBox = document.getElementById("textBox");
-
var spanObj = document.getElementById("charsLeft");
-
var charsLeft = remaining - textBox.value.length;
-
if (charsLeft <= 0) {
-
charsLeft = 0;
-
textBox.value = textBox.value.substring(0,remaining);
-
}
-
spanObj.innerHTML = charsLeft + " characters left.";
-
}
Thanks
Actually I was doing in this way. This code is working but not as I wanted. -
function left()
-
{
-
var maxlen=20;
-
var len=document.che.firstname.value.length;
-
if(len>maxlen)
-
{
-
return false;
-
exit();
-
}
-
else
-
{
-
var str=document.che.firstname.value.length;
-
var str=maxlen-str;
-
document.write(str+"charaters left");
-
}
-
}
-
<form name="che">
-
<input type="text" value="" name="firstname" onkeyup="left()" />
So now I came to know where I was wrong.
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.
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.
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.
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. - //html
-
<input name="firstname"type="text" id="textBox" value="" size="20" maxlength="20" /><span id="charsLeft" >20 Characters Left</span>
-
-
<input name="middlename"type="text" id="textBoxm" value="" size="20" maxlength="20" /><span id="charsLeftm" >20 Characters Left</span>
-
-
<input name="lastname"type="text" id="textBoxl" value="" size="20" maxlength="20" /><span id="charsLeftl" >20 Characters Left</span>
-
-
acoder 16,027
Recognized Expert Moderator MVP
The only changes you need to make are in the first three lines of the function: - function charCount(remaining, id) {
-
var textBox = document.getElementById("textBox"+id);
-
var spanObj = document.getElementById("charsLeft"+id);
and in the HTML: -
<input name="firstname" type="text" id="textBox" value="" size="20" maxlength="20" onkeyup="charCount(this.maxLength,'')" /><span id="charsLeft" >20 Characters Left</span>
-
-
<input name="middlename" type="text" id="textBoxm" value="" size="20" maxlength="20" onkeyup="charCount(this.maxLength,'m')" /><span id="charsLeftm" >20 Characters Left</span>
-
-
<input name="lastname" type="text" id="textBoxl" value="" size="20" maxlength="20" onkeyup="charCount(this.maxLength,'l')" /><span id="charsLeftl" >20 Characters Left</span>
The only changes you need to make are in the first three lines of the function: - function charCount(remaining, id) {
-
var textBox = document.getElementById("textBox"+id);
-
var spanObj = document.getElementById("charsLeft"+id);
and in the HTML: -
<input name="firstname" type="text" id="textBox" value="" size="20" maxlength="20" onkeyup="charCount(this.maxLength,'')" /><span id="charsLeft" >20 Characters Left</span>
-
-
<input name="middlename" type="text" id="textBoxm" value="" size="20" maxlength="20" onkeyup="charCount(this.maxLength,'m')" /><span id="charsLeftm" >20 Characters Left</span>
-
-
<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.
acoder 16,027
Recognized Expert Moderator MVP
You're welcome. Glad to hear that it worked as you wanted.
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.
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.
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!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |