Computer >> Computer tutorials >  >> Programming >> HTML

How to limit the number of characters entered in a textarea in an HTML form?


To add a multi-line text input, use the HTML <textarea> tag. You can set the size of a text area using the cols and rows attributes. To limit the number of characters entered in a textarea, use the maxlength attribute. The value if the attribute is in number.

Here are the other attributes of <textarea> tag −

Attribute Value Description
autofocusHow to limit the number of characters entered in a textarea in an HTML form? autofocus Specifies that on page load the text area should automatically get focus.
Cols number Specifies the width of the textarea based on the number of visible character widths
Disabled disabled Specifies the width of the textarea based on the number of visible character widths.
formHow to limit the number of characters entered in a textarea in an HTML form? form_id Specifies one or more forms.
maxlengthHow to limit the number of characters entered in a textarea in an HTML form? number Specifies the maximum number of characters in textarea.
Name text Assigns a name to the input control.
placeholderHow to limit the number of characters entered in a textarea in an HTML form? text Specifies a short hint of the value in textarea.
Readonly readonly Sets the input control to read-only. It won't allow the user to change the value. The control however, can receive focus and are included when tabbing through the form controls.
requiredHow to limit the number of characters entered in a textarea in an HTML form? required Specifies that a textarea is required.
Rows number Specifies the height of the textarea based on the number of visible lines of text. If there's more text than this allows, users can scroll using the textarea's scrollbars.
wrapHow to limit the number of characters entered in a textarea in an HTML form? hard soft Specifies the text to be wrapped in textarea.

 

How to limit the number of characters entered in a textarea in an HTML form?

You can try to run the following code to limit the number of characters entered in textarea in an HTML form −

<!DOCTYPE html>
<html>
   <head>
      <title>HTML textarea Tag</title>
   </head>

   <body>
      <form action = "" method = "get">
         What improvements you want in College?
         <br>
      
         <textarea rows = "5" cols = "40" maxlength = "100" name = "description">
            Enter answer here...
         </textarea>
         <br>
         
         <input type = "submit" value = "submit" />
      </form>
      
   </body>
</html>