References

Beginner-friendly references for web development, with live, editable examples.

The HTML minlength attribute

Attribute All modern browsers Updated
In short

The HTML minlength attribute sets the minimum number of characters a text field accepts. Add it to an <input> or <textarea>, as in minlength="8" on a password field. A shorter entry fails validation and blocks submission, but an empty field still passes unless you also add required.

Overview

The HTML minlength attribute sets the minimum number of characters a user must enter in a text field. It works on <textarea> and on <input> fields of type text, search, url, tel, email and password. The value is a whole number, 0 or higher.

The limit feeds the browser's built-in constraint validation. When someone types fewer characters than required, the field matches the :invalid pseudo-class, validity.tooShort becomes true, and pressing submit shows an error instead of sending the form. Chrome's message asks the user to lengthen the text and says how many characters they have used.

Two rules explain most surprises. An empty field always passes, because minlength only applies once there is a value, so pair it with required when the field can't be blank. The check also covers only what the user typed, which means a value set from JavaScript is never flagged as too short.

Length is counted in UTF-16 code units, the same way JavaScript's length property counts. Spaces count like any other character, a line break in a textarea counts as one, and a character that needs two code units, such as many emoji, counts as two. Number inputs ignore minlength entirely.

Syntax

<label for="pw">Password</label>
<input type="password" id="pw" name="password" minlength="8" autocomplete="new-password" required>

Values

Value
A non-negative integer.

Example

Live example
<form onsubmit="event.preventDefault(); document.getElementById('msg').textContent = 'Username accepted';">
  <p>
    <label for="user">Username, at least 4 characters</label>
    <input type="text" id="user" name="user" minlength="4" required>
    <button>Check</button>
  </p>
  <p id="msg"></p>
</form>

Best practices

  • Add required next to minlength when a field must not be empty. An empty value passes the length check on its own.
  • State the rule in visible text, such as a hint reading At least 8 characters, and connect it to the field with aria-describedby.
  • Keep minlength lower than or equal to maxlength. A higher minimum makes every non-empty value invalid.
  • Limit numbers with min and max, because type="number" ignores minlength.
  • Run the same length check on the server, where the browser's rule can't be skipped.

Accessibility

Tell people about the minimum before they start typing. MDN notes that some browsers display an error message only once submission fails. A hint placed next to the field and referenced with aria-describedby is linked to the field as its description, so assistive technology users can reach the rule right away.

Frequently asked questions

What does the minlength attribute do in HTML?
The minlength attribute sets the fewest characters a text field will accept. If the user types fewer, the browser marks the field invalid and refuses to submit the form.
Why is minlength not working on my input?
The minlength check skips empty fields, values set by script and number inputs. Add required to reject an empty field, and use min and max to limit a number instead.
Does minlength work on a textarea?
Yes, minlength works on a textarea as well as on text-like input types. Each line break in a textarea counts as one character toward the limit.
Does minlength count spaces?
Yes, minlength counts spaces like any other character. Eight spaces satisfy minlength=8, so trim and check the value on the server if blank input matters.
What is the difference between minlength and maxlength?
The minlength attribute sets a lower length limit, the opposite of maxlength. Browsers stop typing at the maxlength limit, but a value below minlength can still be typed and is reported as an error on submit.
Is minlength enough to enforce a minimum password length?
No, minlength only guides people using a normal browser. Anyone can remove the attribute or send the request directly, so repeat the minlength rule on your server.