maxlength

The HTML maxlength attribute limits the number of characters a user can input in a text field, ensuring that the input remains within a specified length.

maxlength attribute

  • Purpose: The maxlength attribute sets the maximum number of characters that a user can input in an <input> or <textarea> element.

Syntax

index.html
<input type="text" maxlength="value">

Example

index.html
<label for="username">Username (max 10 characters):</label>
<input type="text" id="username" name="username" maxlength="10">

Definition and Usage

The maxlength attribute sets the maximum number of characters permitted in an <input> element.

Values

  • number
    • The highest allowable number that can be entered in an input field.

Applies To

The maxlength attribute is applicable to the following HTML elements.

See also

Conclusion

The maxlength attribute restricts the number of characters a user can enter into a text input or textarea. This helps ensure that the input remains within the specified length. It enhances form validation and user experience by limiting excessive input.