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 2 of 2
  1. #1
    Senior Coder
    Join Date
    Jun 2002
    Location
    41 8' 52" N -95 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Maxtext v1.2 (cross-browser textarea maxlength script)

    Code:
    <script language="javascript" type="text/javascript">
    <!--
    /*********************
    Maxtext v1.2
     2002 Robert K. Davis
    **********************/
    function textlen(x,y){
      var thelength = x.value.length;
      window.status=thelength+' of '+y+' maximum characters.';
    }
    
    function maxtext(x,y){
    tempstr = x.value
      if(tempstr.length>y){
        x.value = tempstr.substring(0,y);
      }
      textlen(x,y);
    }
    /*********************/
    // -->
    </script>
    
    <form name="myform">
         <textarea      name="mytextarea" 
                               cols="45" 
                               rows="3" 
                               wrap="virtual" 
                               onkeypress="return(this.value.length<20)"
                               onkeydown="textlen(this,20)" 
                               onkeyup="textlen(this,20)" 
                               onblur="maxtext(this,20)"
         ></textarea>
    </form>
    I know that's a lot of onkeystuff

    The ideal solution would be not to use a script and just use:

    onkeypress="return(this.value.length<20)"

    However, that only works in IE and Netscape - Opera completely ignores it.

    To keep it simple and work in all browsers (at least the ones I have, that is!), that was the easiest solution I could come up with. This script was tested in Opera 6, Netscape 4.x, IE 4-6 on PC, and IE 4(I think) on MAC. If you have the opportunity to test it out in another browser, please let me know the results (good or bad).

    In Opera, it will not limit the textarea until the onblur() kicks in. In Opera AND Netscape, you can paste past the maxlength of the textarea, which is corrected when the onblur() kicks in.

    Every single other script of its type that I've tried will produce unexpected and sometimes very amusing results (like typing backwards) in those browsers.
    Last edited by whammy; 08-22-2002 at 12:12 AM.

  • #2
    New to the CF scene
    Join Date
    Feb 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks for this script... this looks very nice and I could test it successfully

    Howewer, how is it possible to:
    - Stop the user from typing when he has reached the maxsize for the textarea?? In the test I made, the user can continue typing, even if he is warned that the maxsize has been reached...
    - Pop-up a message to the user when he tries to write more than the maxsize...

    Thanks


  •  

    Posting Permissions

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