Results 1 to 5 of 5

Thread: maxlength on textarea?

  1. #1

    Thread Starter
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236

    Cool maxlength on textarea?

    Anybody know if you can set a maxlength on a textarea? I tried just slapping maxlength='150' in it but it didn't a work.

  2. #2
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    I really like this approach to the problem:
    http://www.supermegaphone.com/message.htm

    For this textarea, when you type, you see a field that counts down the number of characters remaining. With innerHTML, you wouldn't even have to have the number being displayed in a field, you could just place the number-of-characters-remaining right next to the textarea.

    cudabean

  3. #3
    Fanatic Member punkpie_uk's Avatar
    Join Date
    Sep 2001
    Location
    UK
    Posts
    645
    Theres no attribute for this, you'll have to use a client side script to do it

    Code:
    <html>
    <head>
    <title>Validating Text Area value</title>
    <script language="JavaScript">
    var txtLimit = 10
    function limitChar(){
    	var txtAreaString = document.forms['formName'].txtAreaName.value
    	if (txtAreaString.length > txtLimit){
    		alert("Your entry is " + txtAreaString.length + 
                          "characters long." +
    		      "  Limit the text area to " + txtLimit + 
                          " characters.  Please try again.")
    		document.forms['formName'].txtAreaName.focus()
    		return false 
    	} return true
    } 
    </script>
    </head>
    
    <body>
    FormFieldName: (Character Limit:&nbsp;
    <script language="JavaScript">
    	document.write(txtLimit)
    </script>
    )
    <form name="formName" onSubmit="return limitChar()">
    	<textarea name="txtAreaName" cols="50" rows="10"></textarea>
    	<input type="submit" name="submit" value="Submit">&nbsp;&nbsp;
    	<input type="reset" name="reset" value="Reset">
    </form>
    </body>
    
    </html>
    SPREAD THE WORD!!! Are You Lee McCormick? Because I Am



    Lee M McCormick
    [email protected]

    Lee McCormick.com - Live
    Dynamically Webbed.com - In development but live

  4. #4
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    I like the countdown thing too, here is a very simple version
    Code:
    <html>
    <head>
    <SCRIPT Language = "JavaScript">
    function UpdateCharCount() {
        intChars = 255 - document.form1.Comments.value.length;
        document.form1.Remaining.value = intChars;
    }
    </SCRIPT>
    </head>
    <body>
    <form name="form1" action="" method="post">
    <textarea name="Comments" rows="3" cols="20" onKeyUp="javascript:UpdateCharCount();"></textarea>
    <br>
    <input type="text" name="Remaining" value="255"  size="3" maxlength="3" disabled>
    </form>
    </body>
    </html>
    Tested in IE6, NS6.2 and Opera 6

    change 255 to whatever you want the limit to be

  5. #5

    Thread Starter
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236
    Great, thats what I needed. I'm surprised they dont have a property of the textarea for this already but cuttin and pastin script ain't too hard either. 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
  •  



Click Here to Expand Forum to Full Width