👀 Live Preview
Password field with minlength="8" and live character count:
0 characters (minimum 8 required)

The minlength attribute requires a minimum number of characters in a text-like <input> or <textarea> before the field passes validation. Unlike min (which sets numeric or date/time floors), minlength counts letters, digits, spaces, and symbols. Users can still type short text, but the browser marks the field invalid on submit or when checkValidity() runs. Use it for passwords, usernames, comments, and any field where a minimum text length is required.
Count, not value.
Primary elements.
e.g. minlength="8".
Chars vs numbers.
Upper bound pair.
Update in JS.
minlength AttributeThe primary purpose of minlength is to enforce a lower bound on text length at validation time. A password field with minlength="8" rejects submissions with fewer than eight characters. A username with minlength="3" ensures at least three characters before the form is accepted.
Unlike maxlength, which stops the user from typing past a limit, minlength does not block keystrokes—it validates when the form is submitted or when constraint validation runs. Pair it with required so empty fields fail too, and complement it with server-side length checks.
minminlength="8" requires eight characters in a password field. min="8" on type="number" requires a numeric value of at least 8. Use the right attribute for the input type.
Add minlength with a non-negative integer to text-like inputs or textarea:
<label for="username">Username:</label>
<input type="text" id="username" minlength="3" maxlength="20">
<label for="password">Password:</label>
<input type="password" id="password" minlength="8">
<label for="comment">Comment:</label>
<textarea id="comment" minlength="10" maxlength="500"></textarea>minlength="8".input types: text, search, url, tel, email, password.<textarea> elements.number, date, checkbox, radio, etc.—use min for those.maxlength for a character length range; ensure minlength ≤ maxlength.element.minLength = 8 (camelCase, number).The minlength attribute accepts a non-negative integer:
minlength="3" — Username must have at least three characters.minlength="8" — Common minimum for passwords (combine with stronger server rules).minlength="10" — Short comment or bio minimum.minlength="0" — No minimum enforced (same as omitting the attribute in practice).document.getElementById("dynamicField").minLength = 10;
document.getElementById("password").setAttribute("minlength", "12");| Use case | Markup | Notes |
|---|---|---|
| Password | type="password" minlength="8" | Min char count |
| Username | <input minlength="3" maxlength="20"> | Length range |
| Comment box | <textarea minlength="10"> | Multiline minimum |
| Numeric floor | Use min, not minlength | Different attribute |
| Min + max length | minlength="8" maxlength="64" | Password rules |
| JS update | el.minLength = 5 | Number property |
| Element / type | Supported? | Notes |
|---|---|---|
<input type="text"> | Yes | Most common use |
<textarea> | Yes | Multiline minimum length |
email, url, tel, search, password | Yes | Text-like input types |
<input type="number"> | No | Use min for value limit |
date, time, checkbox, radio | No | Not a character-count attribute |
minlength vs maxlength vs min| Attribute | Applies to | Limits |
|---|---|---|
minlength | text, email, password, textarea, etc. | Minimum character count (validation) |
maxlength | Same text-like inputs and textarea | Maximum character count (blocks typing) |
min | number, range, date, time, meter, progress | Minimum numeric or date/time value |
Password minimum length, signup form with username and password rules, and dynamic minLength with JavaScript.
Password field with minlength="8" and live character count:
0 characters (minimum 8 required)
Require at least eight characters in a password field:
<label for="password">Password:</label>
<input type="password" id="password" minlength="8" required>minlength validates on submit and when checkValidity() runs. It does not prevent typing fewer characters unlike maxlength.
Apply different minimums to username and password:
<form>
<label for="username">Username:</label>
<input type="text" id="username" minlength="3" required>
<label for="password">Password:</label>
<input type="password" id="password" minlength="8" required>
<button type="submit">Sign up</button>
</form>Each control enforces its own minimum independently. Combine with maxlength where an upper cap also applies.
Change the minimum character requirement when rules change at runtime:
<input type="text" id="dynamicField" minlength="3">
<script>
document.getElementById("dynamicField").minLength = 10;
</script>The IDL property minLength (camelCase, number) mirrors the content attribute. The old reference set 5 in code but described 10—keep code and text aligned.
required — minlength on an empty field may still pass unless the field is required or the user interacted with it.Integer character floor.
Any length allowed.
Count checked on submit.
Field passes check.
The minlength attribute is fully supported on all listed text-like inputs and <textarea> in modern browsers. Constraint validation behavior is consistent across Chrome, Firefox, Safari, and Edge.
All major browsers enforce minlength on text inputs and textarea.
Bottom line: Safe to use on supported elements; validate length on the server too.
maxlength when both bounds matterrequired when the field cannot be emptyminlength on type="number" (use min)minlength with minThe minlength attribute is a practical way to require a minimum amount of text in form fields. Applied to text-like inputs and textarea elements, it improves data quality by catching too-short input at validation time.
Choose sensible minimums, explain them in labels, distinguish minlength from min and maxlength, and always enforce length on the server. Used thoughtfully, it keeps forms useful and databases cleaner.
minlengthBookmark these before building text forms.
Not numeric min.
MeaningSupported elements.
ScopeMin vs max chars.
CompareJS number property.
DynamicClient ≠ secure.
Securityminlength limits character count on text fields. min limits numeric or date/time values on number, range, date, and similar inputs.input types text, search, url, tel, email, and password, plus <textarea>. Not number, date, checkbox, or radio.element.minLength = 8 (camelCase number) or setAttribute("minlength", "8").maxlength caps how many characters can be typed. minlength sets the minimum required for validity. Use both to define a text length range.Practice password rules, signup forms, and dynamic minLength in the Try It editor.
5 people found this page helpful