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

HTMLWeb DevelopmentFront End Technology

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.

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>
raja
Published on 23-Jan-2018 07:53:01
Advertisements