Results 1 to 6 of 6

Thread: Limit the character length of a <textarea> field? [resolved]

  1. #1

    Thread Starter
    Addicted Member Jez1's Avatar
    Join Date
    Oct 2001
    Location
    Warwickshire, England
    Posts
    185

    Limit the character length of a <textarea> field? [resolved]

    How can I limit the length, in characters, of a <textarea> field. I have tried the MAXLENGTH= property, but it seems to have no effect.

    Thanks

    Jez
    Last edited by Jez1; May 19th, 2004 at 09:48 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    In the onKeyDown event, call a function that checks the length of the value of the textarea.

    Code:
    //let's suppose, maxlimit = 100
    
    if (textareaname.value.length > maxlimit) 
    
    textareaname.value = textareaname.value.substring(0, maxlimit);
    HTH

  3. #3
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Yeah, I was trying this:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    function doIt(_obj) {
    /*
    Couldnd't get this working:
    if (the_key_pressed_in_ascii == 8) //Backspace
    	{
    	return true
    	}
    */
    if (_obj.value.length > 15)
    	{
    	return false
    	}
    }
    </script>
    </head>
    
    <body>
    <textarea onKeyPress="return doIt(this)"></textarea>
    </body>
    </html>
    but your code is robably better.
    Have I helped you? Please Rate my posts.

  4. #4

    Thread Starter
    Addicted Member Jez1's Avatar
    Join Date
    Oct 2001
    Location
    Warwickshire, England
    Posts
    185
    I'm guessing that's Javascript.
    Could you provide a simple explaination of how to put that code into a function and how to call it on the onKeyDown event please?

    Javascript is confusing to me

  5. #5
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    using menhaks code:
    Code:
    <script type="text/javascript">
    function doIt(_obj) {
    //let's suppose, maxlimit = 100
    maxlimit = 100
    if (_obj.value.length > maxlimit) 
    _obj.value = _obj.value.substring(0, maxlimit);
    }
    </script>
    <textarea onKeyDown="return doIt(this)"></textarea>
    Have I helped you? Please Rate my posts.

  6. #6

    Thread Starter
    Addicted Member Jez1's Avatar
    Join Date
    Oct 2001
    Location
    Warwickshire, England
    Posts
    185
    Thanks, it works great.

Posting Permissions

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



Click Here to Expand Forum to Full Width