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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Max size of characters allowed to type in textarea

    For a textarea how do I limit the number of characters in the box to 500?

    For input text I can set to maxlength and limit the entry in the input: <input type="text" maxlength="500" name="myfield">
    For textarea I assume I need to create a validation or something to fetch the length of the field and stop the entry after 500 characters. Please advise how this can be done.

  • #2
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    362
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks it works but I also want a alert message and cant seem to get it to work because not alert message appears once the 500 characters is entered.
    Please advise:
    Code:
    <script language="javascript" type="text/javascript">
    function limitText(limitField, limitCount, limitNum) {
    	if (limitField.value.length > limitNum) {
    		limitField.value = limitField.value.substring(0, limitNum);
                                    alert("You are trying to enter more than 500 characters");
    	} else {
    		limitCount.value = limitNum - limitField.value.length;
    	}
    }
    </script>
    ----------
    
    <form name="myform">
    <textarea name="limitedtextarea" onKeyDown="limitText(this.form.limitedtextarea,this.form.countdown,500);" 
    onKeyUp="limitText(this.form.limitedtextarea,this.form.countdown,500);">
    </textarea>

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,022
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Over-complicated. Try this:-

    <script type="text/javascript">

    function limitText(limitField) {
    fldValue = limitField.value;
    var limitNum = 500;
    var chars = limitNum - fldValue.length;
    alert (chars); // delete after testing
    if (chars <= 0) {
    alert ("You are trying to enter more than the limit of " + limitNum + " characters! ");
    fldValue = fldValue.substring(0,limitNum-1)
    document.myform.limitedtextarea.value = fldValue;
    }
    if (chars == 20) {
    alert ("You are approaching the limit of " + limitNum + " characters and have only 20 characters left! ")
    }
    }

    </script>

    <form name="myform">
    <textarea name="limitedtextarea" onKeyDown="limitText(limitedtextarea)";
    onKeyUp="limitText(limitedtextarea)"></textarea>
    </form>


  •  

    Posting Permissions

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