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

How to limit the number of characters allowed in form input text field?


The HTML <input> tag is used to get user input in HTML. To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively.

To limit the number of characters, use the maxlength attribute. The max and min attributes are used with number, range, date, datetime, datetime-local, month, time and week input types.

How to limit the number of characters allowed in form input text field?

Example

You can try to run the following code to limit the number of characters allowed in form input text field:

<!DOCTYPE html>
<html>
   <head>
      <title>HTML maxlength attribute</title>
   </head>
   <body>
      <form action = "/cgi-bin/hello_get.cgi" method = "get">
         Student Name:
         <br>
         <input type = "text" name = "first_name" value = "" maxlength = "40" />
         <br>
         Student Subject: <br>
         <input type = "text" name = "last_name" value = "" maxlength = "30" />
         <br>
         <input type = "submit" value ="Submit" />
      </form>
   </body>
</html>