Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-30-2012, 09:00 PM   PM User | #1
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
This code quit working

This code originally worked, but now it doesn't. It's suppose to count the number of characters you type. The form processes fine, but it's not counting, it always says 1,000 in the box. Any ideas? Thanks

Code:
<script type = "text/javascript">

var maxLen = 1000; // max number of characters allowed  // global variable
var max = "You may enter up to " + maxLen + " characters";  // global variable

function OnPaste () {
//return false;   // cancels (blocks)  the onpaste event.  Uncomment this line to block pasting
setTimeout(checkMaxInput,100);  // delay is necessary
}

function initCount() {
document.getElementById("limit").innerHTML = max;
document.getElementById("remLen").value = maxLen;
}

function checkMaxInput() {
var form = document.myform;  // or document.forms[0] or document.getElementById("myform");
if (form.txtarea.value.length > maxLen) {   // if too long.... trim it!
form.txtarea.value = form.txtarea.value.substring(0, maxLen);
document.getElementById("message").innerHTML = "Too many characters were entered!!  The excess over " + maxLen + " have been removed.";
}
else {
document.getElementById("message").innerHTML = "";
}
form.remLen.value = maxLen - form.txtarea.value.length;
}

</script>


<body onload = "initCount()">



<form name = "myform" id = "myform">
<span id = "limit"  style="font-size: 10pt;color: #FF0000;font-family: arial, helvetica, sans-serif;"></span>
<br>
<textarea name="txtarea" wrap=physical cols=48 rows=10  onkeyup="checkMaxInput()"  onblur="checkMaxInput()" onpaste="OnPaste ()" >
</textarea>
<br>
<input readonly type=text name=remLen id = "remLen" size=3 value = ""> characters left</font>
</form>
<span id = "message" style="color:red";></span>
__________________
Been a sign maker for 5 years. My business:
American Made Signs
myfayt is offline   Reply With Quote
Old 04-30-2012, 09:30 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
I like the onPaste() function; but otherwise it seems like a lot of code for such a simple operation.

Are you sure that the form ID is correct? Field IDs?

I prefer the following for character limit:
Code:
function textCounter(field,countfield,maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
		}
	else {
		countfield.value = maxlimit - field.value.length;
		}
	}
PHP Code:
<form id="thisForm">
There are <input type="text" id="remLen" style="border:0px; width:30px;" /> characters remaining<br />
<
textarea id="message" cols=40 rows=4 onKeyUp="textCounter(this,document.forms['thisForm'].remLen,1000);" onKeyDown="textCounter(this,document.forms['thisForm'].remLen,1000);" onKeyPress="textCounter(this,document.forms['thisForm'].remLen,1000);"></textarea>
</
form
If you need something that will run on page load, just add the following to the body tag:
onLoad="textCounter(document.forms['thisForm'].message,document.forms['thisForm'].remLen,1000);"
WolfShade is offline   Reply With Quote
Old 04-30-2012, 09:43 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Works fine in Firefox, with or without Firebug running.

Excepting only that if you CTRL-V to paste in the last batch of characters it doesn't show the "too many" message. But it seems to chop it fine at 1000.

I think because the onpaste fires, which shows the message, but then the onkeyup fires and sees there are exactly 1000 characters and says "gee, everything is fine".

Ahh! Thought of a way to test that theory.

Yep, I was right. I changed this line
Code:
        document.getElementById("message").innerHTML = "";
to this
Code:
        document.getElementById("message").innerHTML += "OK";
And indeed "OK" gets appended to the "too many" message after a paste.

If it's not working for you, show the URL of the page with the problem. I'll bet that some other JS code is interfering. Your <body onload="...">, for example, could be getting wiped out.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 04-30-2012, 09:44 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
As you say, this script worked fine when I originally gave it to you. Suggest you go back to that thread, and copy the code once again.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 04-30-2012, 10:29 PM   PM User | #5
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
It may be better to show you.

http://americanmadesigns.com/newbil/account.php
Username: test4
Password: 12345

Don't worry it doesn't do anything right now. But see if it works for you, I am using Firefox and trying on Windows 7 and Windows XP.
__________________
Been a sign maker for 5 years. My business:
American Made Signs
myfayt is offline   Reply With Quote
Old 04-30-2012, 11:29 PM   PM User | #6
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 950
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
Also, you have a form nested within a form.
WolfShade is offline   Reply With Quote
Old 04-30-2012, 11:29 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You do not *HAVE* any <form> named "myform". Nor do you have one with an id of "myform".

And that's because you have the tag
Code:
<form name = "myform" id = "myform">
ILLEGALLY nested INSIDE of the other <form> which starts at
Code:
<form action="freeposts.php" method="post" enctype="multipart/form-data">
YOU CAN NOT NEST <form>s!

Incidentally, the reason document.forms[0] wouldn't work is because *THAT* refers to your <form> that starts with
Code:
<form method="get" action="">
That is, your search form at the top of the page.

And, by the way, if you expected to submit the textarea contents to your freeposts.php page, that wouldn't happen, either, since it is not a legal part of the form with that action and is instead in the illegally nested <form>.

*********

Wolfshade beat me but only because I'm so long-winded. <grin/>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 05-01-2012, 12:20 AM   PM User | #8
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
I don't get it, the form is closed for the search long before the textarea form is opened. So it's not nested?

PHP Code:
    <div id="logo">
            <
h1><a href="#">Buy Local</a></h1>
            <
pHelp the Local Economy</p>
        </
div>
        <
div id="search">
            <
form method="get" action="">
                <
fieldset>
                    <
input type="text" name="s" id="search-text" size="15" />
                    <
input type="submit" id="search-submit" value="SEARCH" />
                </
fieldset>
            </
form>
        </
div>
    </
div>

    <
div id="menu">
        <
ul>
            <
li class="current_page_item"><a href="../index.html">Home</a></li>
            <
li><a href="../account.php">Your Account</a></li
__________________
Been a sign maker for 5 years. My business:
American Made Signs
myfayt is offline   Reply With Quote
Old 05-01-2012, 12:39 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Answer removed. Clearly it didn't meet Fayt's needs so no point in showing it.

Last edited by Old Pedant; 05-01-2012 at 08:40 PM..
Old Pedant is offline   Reply With Quote
Old 05-01-2012, 02:40 AM   PM User | #10
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
Stop being such a jerk about it. I am trying here.
__________________
Been a sign maker for 5 years. My business:
American Made Signs
myfayt is offline   Reply With Quote
Old 05-01-2012, 07:27 AM   PM User | #11
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by myfayt View Post
Stop being such a jerk about it. I am trying here.
myfayt - you will not win friends and influence people if you insult experienced people who are trying to help you. Old Pedant has explained very clearly what is wrong with you code. You ought to thank him, not abuse him. How about apologising?

"Thou callest me a dog before thou hast cause. But since I am a dog, beware my fangs. " -William Shakespeare, The Merchant of Venice
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 05-01-2012, 07:43 AM   PM User | #12
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Old Pedant View Post
Works fine in Firefox, with or without Firebug running.

Excepting only that if you CTRL-V to paste in the last batch of characters it doesn't show the "too many" message. But it seems to chop it fine at 1000.

I think because the onpaste fires, which shows the message, but then the onkeyup fires and sees there are exactly 1000 characters and says "gee, everything is fine".
That seems to be solved by changing this line to:-

if (form.txtarea.value.length >= maxLen) { // if too long.... trim it!

That also overcomes a stange bug where if the user clicks on the error message it disappears.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 05-01-2012 at 07:47 AM..
Philip M is offline   Reply With Quote
Old 05-01-2012, 08:42 PM   PM User | #13
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I think you are right, Philip. But it's just another hack on top of a hack.

Since I never did like the onkeydown methodology for this problem, I'll get my face out of here.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 05-02-2012 at 12:26 AM..
Old Pedant is offline   Reply With Quote
Old 05-01-2012, 11:49 PM   PM User | #14
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
Quote:
Originally Posted by Philip M View Post
myfayt - you will not win friends and influence people if you insult experienced people who are trying to help you. Old Pedant has explained very clearly what is wrong with you code. You ought to thank him, not abuse him. How about apologising?

"Thou callest me a dog before thou hast cause. But since I am a dog, beware my fangs. " -William Shakespeare, The Merchant of Venice
No if you had read it before he deleted it, it was really bad, he was being immature about it and getting a nasty attitude with me simply because I am not as experienced as the rest of you.

It takes me longer to learn things. But instead of being patient, I get yelled at for not understanding it on the first go.

I will just find someone else for help, since clearly everyone is against me in this thread.
__________________
Been a sign maker for 5 years. My business:
American Made Signs
myfayt is offline   Reply With Quote
Old 05-02-2012, 08:24 AM   PM User | #15
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by myfayt View Post
No if you had read it before he deleted it, it was really bad, he was being immature about it and getting a nasty attitude with me simply because I am not as experienced as the rest of you.

It takes me longer to learn things. But instead of being patient, I get yelled at for not understanding it on the first go.

I will just find someone else for help, since clearly everyone is against me in this thread.
I did see Old Pedant's response, and although he used capitals for emphasis I don't think he was shouting at you or had anything which could be described as a "nasty attitude". In fact in my experience Old Pedant shows extraordinary patience with people who are a little slow on the uptake - sorry, those who take longer to learn things.

It is never a good idea to insult people who are trying to help you. You could have expressed yourself in a more adult way. Nor is it an adult reaction to assert that "everyone is against me in this thread". The outcome is, as you say, that you will now have to look for help elsewhere. Who's loss is that?
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:58 AM.


Advertisement
Log in to turn off these ads.