How do we add the maximum number of characters allowed in an element in HTML?


In HTML, user input is obtained via the <input> tag. The min and max properties, which indicate a maximum and minimum value for an input field, are used to limit the input field. Use the maxlength attribute to restrict the number of characters.

Syntax

<input maxlength="number">

Following are the examples…

Example

In the following example we are using the maxlength attribute with the input type to allow maximum number of elements.

<!DOCTYPE html> <html> style> body { text-align: center; } </style> <body> <form> UserId: <input type="text" name="username" maxlength="9"><br><br> Password: <input type="password" name="password" maxlength="4"><br><br> <input type="submit" value="Submit"> </form> </body> </html>

Output

On executing the above script, the webpage will display an input type for userid and password, which are mentioned with a max length to limit the input elements, along with a submit button.

Example

In this example, let us look at the form contents with input having the max input characters;

<!DOCTYPE html> <html> <body> <form action = ""> Rank: <input type = "number" name = "rank" min = "1" max = "10"><br> Cricketer: <input type = "text" name = "cricketer" maxlength = "20"><br> <input type = "submit" value = ”Submit”> </form> </body> </html>

Output

On executing the above script, the webpage will display an input type for Rank ID and cricketer, which are mentioned with a max length to limit the input elements, along with a submit button.

Updated on: 06-Sep-2022

201 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements