HTML maxlength Attribute
It specifies the maximum number of characters that have been allowed in the Element.
It can be used on the following Elements:
Attribute Values: It contains a single value number that allows the maximum number of characters in the <input> element. Its default value is 524288.
Examples: In this example, we center a form with “Username” and “Password” fields. The maxlength attribute limits input to 12 characters for the username and 10 for the password.
<!DOCTYPE html>
<html>
<body>
<center>
<h1 style="color:green;font-style:italic;">
GeeksForGeeks
</h1>
<h2 style="color:green;font-style:italic;">
maxlength attribute
</h2>
<form action="">
Username:
<input type="text" name="usrname" maxlength="12">
<br>
<br>
Password:
<input type="text" name="password" maxlength="10">
<br>
<br>
<input type="submit" value="Submit">
</form>
</center>
</body>
</html>
Output:
Example : In this example we centers a textarea with a 4×50 size and limits input to 6 characters using the maxlength attribute. The textarea contains placeholder text for user input.
<!DOCTYPE html>
<html>
<body>
<center>
<h1 style="color:green;font-style:italic;">
GeeksforGeeks
</h1>
<h2 style="color:green;font-style:italic;">
maxlength attribute
</h2>
<textarea rows="4" cols="50" maxlength="6">
write here something that you want....
</textarea>
</center>
</body>
</html>
Output:
Supported Browsers: The browsers supported by maxlength attribute are listed below:
HTML maxlength Attribute – FAQs
Which input types support the maxlength attribute?
The maxlength attribute is supported by text, password, email, search, tel, and url input types, as well as the <textarea> element.
What happens if the input exceeds the maxlength value?
If the input exceeds the maxlength value, additional characters will not be accepted or displayed in the input field.
Is the maxlength attribute required?
No, the maxlength attribute is optional. Use it only when you need to limit the number of characters a user can enter in an input field.
Does the maxlength attribute count spaces and special characters?
Yes, the maxlength attribute counts all characters, including spaces, special characters, and numbers.
What is the default value of the maxlength attribute if it is not specified?
If maxlength is not specified, there is no limit to the number of characters that can be entered in the input field.