minlength

The HTML minlength attribute sets the minimum number of characters required in an input field, ensuring users provide a certain length of input before submitting the form.

minlength attribute

The minlength attribute in HTML specifies the minimum character length required for an <input> or <textarea> element before a form can be submitted. It is used with input types such as text, email, search, password, tel, and url to ensure the input meets length or strength requirements.

Syntax

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

value

  • The least number of characters needed.

Example

index.html
<label for="password">Password (min 8 characters):</label>
<input type="password" id="password" name="password" minlength="8">

Definition and Usage

The minlength attribute defines the minimum number of characters required in an input field.

The minlength attribute can be applied to <input> elements with types: text, search, url, tel, email, and password.

Key Points

  • Error Display: In some browsers, an error message is shown if the input doesn't meet the minimum character requirement.
  • Optional Fields: The minlength attribute doesn't force the field to be mandatory; an empty field is acceptable unless it's marked as required.

See also

Conclusion

The minlength attribute ensures that users input a minimum number of characters before submitting a form. It is commonly used for fields like passwords and emails to enforce basic input requirements. This attribute helps maintain data integrity and provides better validation for user inputs.