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 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2003
    Location
    Kelowna, British Columbia
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is it possible to set max char on text area input box, stops like <text> will?

    With <text> you can set max length and when user inputs and reaches limit you can't type anything else in... like the subject input fields here. Is it possible to set these attributes on <textarea> input fields?

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try this:
    Code:
    <script type="text/javascript">
    function getLength(l,m){
    window.status = l + " of " + m + " maximum characters.";
    return l < m;
    }
    function maxLength(t,m){
    var l = t.value.length;
    if (l > m){
    alert("Your entry of " + l
    + " characters exceeds the maximum of " + m + ".\n"
    + "It will now be truncated.");
    t.value = t.value.substring(0,m);
    }
    getLength(t.value.length,m);
    }
    </script>
    </HEAD>
    
    <BODY>
    <form>
    <textarea cols="44" rows="12"
    onkeypress="return getLength(this.value.length,200)"
    onkeyup="return getLength(this.value.length,200)"
    onblur="maxLength(this,200)"></textarea>
    </form>
    (change what is highlighted in red to reflect the number of characters to allow)


    .....Willy

  • #3
    New Coder
    Join Date
    Dec 2003
    Location
    Kelowna, British Columbia
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Willy... I will try this. No wonder I was getting stumped on how to do this... I had no idea it would be so involved when its smaller counter part <text> merely uses maxlength=x.

    Cheers!

  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by percept
    I had no idea it would be so involved when its smaller counter part <text> merely uses maxlength=x.
    In all actuallity; it is not that complex.
    This would also work:
    Code:
    <BODY>
    <form>
    <textarea cols="44" rows="12"
    onKeyUp="if (this.value.length>200){this.value=this.value.substring(0,200)}">
    </textarea>
    </form>
    But, it would not be as user friendly.

    The first example does its best to notify the user of the status
    thru the status bar and accounts for pasting into the textarea.

    I suppose, the need would depend upon how savy your users are!

    .....Willy

  • #5
    New Coder
    Join Date
    Dec 2003
    Location
    Kelowna, British Columbia
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The users will probably be like me... wishing they were savy'r

    Thanks again


  •  

    Posting Permissions

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