Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

minlength — minimum text length

The minlength attribute sets the minimum length of a nonempty user-entered value in a textarea or supported input field. It does not make the field mandatory; use required for that.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

minlength="N"

minlength="N" sets a length minimum for textarea and input with type text, search, tel, url, email, or password. It does not apply to input of type number: min limits the numeric value, not text length.

N must be a non-negative integer; if maxlength is also specified, the minimum must not exceed the maximum. Length is measured in UTF-16 code units, not visible characters. In textarea, a normalized newline counts as one unit.

minlength does not replace required: an empty value does not violate the minimum. A nonempty value that is too short after user editing causes validity.tooShort === true. Assigning to value in JavaScript alone does not trigger this violation; it is not equivalent to typing.

The example displays the state through validity.tooShort and checkValidity(). Type abc, append d, and then clear the field. Other constraints, such as address validity for type="email", still apply independently.

Examples

<!doctype html>
<meta charset="utf-8">
<title>minlength</title>
<label for="code">Code (min. 4)</label>
<input id="code" type="text" minlength="4">
<pre id="result">tooShort=false; valid=true</pre>
<script>
  const field = document.getElementById('code');
  const result = document.getElementById('result');
  field.addEventListener('input', () => {
    result.textContent = `tooShort=${field.validity.tooShort}; valid=${field.checkValidity()}`;
  });
</script>

Result

Initial: tooShort=false; valid=true
Type abc: tooShort=true; valid=false
Append d: tooShort=false; valid=true
Clear: tooShort=false; valid=true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources