Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,417
    Thanks
    68
    Thanked 102 Times in 101 Posts

    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 7 years. My business:
    American Made Signs

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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);"

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #5
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,417
    Thanks
    68
    Thanked 102 Times in 101 Posts
    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 7 years. My business:
    American Made Signs

  • #6
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Also, you have a form nested within a form.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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.

  • #8
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,417
    Thanks
    68
    Thanked 102 Times in 101 Posts
    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 7 years. My business:
    American Made Signs

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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.

  • #10
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,417
    Thanks
    68
    Thanked 102 Times in 101 Posts
    Stop being such a jerk about it. I am trying here.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.
    Last edited by Philip M; 05-01-2012 at 07:47 AM.

    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.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,184
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    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.
    Last edited by Old Pedant; 05-02-2012 at 12:26 AM.
    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.

  • #14
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,417
    Thanks
    68
    Thanked 102 Times in 101 Posts
    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 7 years. My business:
    American Made Signs

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •