VOOZH about

URL: https://www.geeksforgeeks.org/html/html-input-maxlength-attribute/

⇱ HTML input maxlength Attribute - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

HTML input maxlength Attribute

Last Updated : 26 May, 2026

The maxlength attribute is used to define the maximum number of characters allowed in an input field. It helps restrict the length of user input.

  • Limits the number of characters a user can enter.
  • Commonly used with text-based input fields like text, password, and email.
  • Helps in input validation and controlling data size.

Syntax:

<input maxlength="number">
  • number: It contains a single value number which allows the maximum number of characters in <input> element. Its default value is 524288.

Examples of Using the maxlength Attribute

Here are some examples of using maxlength attribute:

Example 1: Limiting Username and Password Length

The maxlength attribute is used within <input> elements to restrict the length of text inputs for username and password fields.

Example 2: Limiting Email Address Length

Here, the maxlength attribute limits the length of an email input field to 30 characters.

Comment
Article Tags: