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.
Page 3 of 3 FirstFirst 123
Results 31 to 34 of 34

Thread: Limit textarea?

  1. #31
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,415
    Thanks
    68
    Thanked 102 Times in 101 Posts
    I am sorry that I created such trouble over this topic.
    Been a sign maker for 7 years. My business:
    American Made Signs

  2. #32
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,415
    Thanks
    68
    Thanked 102 Times in 101 Posts
    Quote Originally Posted by Philip M View Post
    USE THIS!!!

    Code:
    <html>
    <head>
    
    <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>
    
    </head>
    
    <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>
    </body>
    </html>
    Ran into a problem, if you try to type more at the end of it, it doesn't allow it, however if you put one up in say the middle, it allows it. Such as those 3's. It doesn't post after 222222222, but above it does.

    But I could just do a character check after submitting. What about trimming the post on submit? so any characters over 1,000 will get removed before it enters the database?
    Attached Thumbnails Attached Thumbnails Limit textarea?-ad.jpg  
    Been a sign maker for 7 years. My business:
    American Made Signs

  3. #33
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,915
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by myfayt View Post
    Ran into a problem, if you try to type more at the end of it, it doesn't allow it, however if you put one up in say the middle, it allows it. Such as those 3's. It doesn't post after 222222222, but above it does.
    Sorry, I do not understand what you are driving at.

    If you reach the limit of 1000 characters (whether by typing or pasting) then no more characters can be added to the textarea. If you try to add another character at the end it will not allow it. If you add a character in the middle of the text (which amounts to an edit) then one character is removed from the end to compensate, still leaving 1000. OK?

    There is no need to repeat the validation onsubmit. But of course the user can always disable Javascript so you must repeat the validation on the server.

    I do hope we have reached the end of this topic now!
    Last edited by Philip M; 04-14-2012 at 04:02 PM. Reason: Noticed typo

    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.

  4. #34
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,415
    Thanks
    68
    Thanked 102 Times in 101 Posts
    After more testing, you are correct, it's working perfectly. Thank you and everyone else so much!
    Been a sign maker for 7 years. My business:
    American Made Signs


 
Page 3 of 3 FirstFirst 123

Posting Permissions

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