HTML Guide for maxlength
The maxlength attribute can be used on an input element to define a client-side validation for the maximum length allowed on an input without resorting to JavaScript.
This attribute is only allowed on elements of type email, password, search, tel, text, or url.
Remove the empty maxlength value and provide a non-negative integer or omit the attribute entirely.
Explanation
The maxlength attribute on an input element must be a valid non-negative integer per the HTML specification. An empty string ("") is invalid and triggers the validator error.
- Valid: a decimal integer like 0, 10, 255.
-
Invalid: empty string, negative numbers, non-numeric strings, or whitespace.
If no maximum length is needed, omit maxlength instead of leaving it empty.
Note that maxlength applies to text-entry controls such as type="text", search, url, tel, email, and password. For other types (e.g., number, date), maxlength is ignored and should not be used.
Examples
Correct: set an explicit maximum
<input type="text" name="username" maxlength="20">
Correct: no maximum, omit the attribute
<input type="text" name="comment">
Incorrect: empty string (validator error)
<input type="text" name="username" maxlength="">
The value of the maxlength attribute contains invalid characters because it is incorrectly combined with another attribute.
In HTML, the maxlength attribute for an input element should contain only a non-negative integer value, which specifies the maximum number of characters allowed in the input. The aria-required attribute must be a separate attribute in the tag.
Correct usage separates each attribute and assigns the appropriate value using quotation marks for each. For example, maxlength="200" and aria-required="true" must be distinct and not combined.
Incorrect:
<input type="text" name="nome" id="nome" maxlength="200 aria-required="true">
Correct:
<input type="text" name="nome" id="nome" maxlength="200" aria-required="true">
This valid example uses maxlength="200" for character limit and adds aria-required="true" to improve accessibility for assistive technologies.