HTML minlength Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Forms & Input

Introduction

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.

What You’ll Learn

01

Character floor

Count, not value.

02

text & textarea

Primary elements.

03

Positive int

e.g. minlength="8".

04

vs min

Chars vs numbers.

05

maxlength

Upper bound pair.

06

.minLength

Update in JS.

Purpose of minlength Attribute

The 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.

💡
Not the same as min

minlength="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.

📝 Syntax

Add minlength with a non-negative integer to text-like inputs or textarea:

minlength.html
<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>

Syntax Rules

  • Value is a non-negative integer (character count), e.g. minlength="8".
  • Valid on input types: text, search, url, tel, email, password.
  • Valid on <textarea> elements.
  • Not valid on number, date, checkbox, radio, etc.—use min for those.
  • Pair with maxlength for a character length range; ensure minlengthmaxlength.
  • JavaScript IDL property: element.minLength = 8 (camelCase, number).

💎 Values

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).
  • Attribute absent — No HTML-enforced minimum character count.
minlength-js.html
document.getElementById("dynamicField").minLength = 10;

document.getElementById("password").setAttribute("minlength", "12");

⚡ Quick Reference

Use caseMarkupNotes
Passwordtype="password" minlength="8"Min char count
Username<input minlength="3" maxlength="20">Length range
Comment box<textarea minlength="10">Multiline minimum
Numeric floorUse min, not minlengthDifferent attribute
Min + max lengthminlength="8" maxlength="64"Password rules
JS updateel.minLength = 5Number property

Applicable Elements

Element / typeSupported?Notes
<input type="text">YesMost common use
<textarea>YesMultiline minimum length
email, url, tel, search, passwordYesText-like input types
<input type="number">NoUse min for value limit
date, time, checkbox, radioNoNot a character-count attribute

minlength vs maxlength vs min

AttributeApplies toLimits
minlengthtext, email, password, textarea, etc.Minimum character count (validation)
maxlengthSame text-like inputs and textareaMaximum character count (blocks typing)
minnumber, range, date, time, meter, progressMinimum numeric or date/time value

Examples Gallery

Password minimum length, signup form with username and password rules, and dynamic minLength with JavaScript.

👀 Live Preview

Password field with minlength="8" and live character count:

0 characters (minimum 8 required)

Example — Password Minimum Length

Require at least eight characters in a password field:

password-minlength.html
<label for="password">Password:</label>

<input type="password" id="password" minlength="8" required>
Try It Yourself

How It Works

minlength validates on submit and when checkValidity() runs. It does not prevent typing fewer characters unlike maxlength.

Example — Signup Form

Apply different minimums to username and password:

form-minlength.html
<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>
Try It Yourself

How It Works

Each control enforces its own minimum independently. Combine with maxlength where an upper cap also applies.

Dynamic Values with JavaScript

Change the minimum character requirement when rules change at runtime:

dynamic-minlength.html
<input type="text" id="dynamicField" minlength="3">



<script>

  document.getElementById("dynamicField").minLength = 10;

</script>
Try It Yourself

How It Works

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.

♿ Accessibility

  • Announce minimums in labels — e.g. “Password (at least 8 characters)” so users know the requirement before typing.
  • Provide clear validation messages — Browser default messages help; custom messages should be plain language.
  • Do not rely on minlength alone — Assistive technology users still need server-validated length rules.
  • Avoid excessive minimums — Very long required lengths frustrate users and may block valid input.
  • Pair with requiredminlength on an empty field may still pass unless the field is required or the user interacted with it.

🧠 How minlength Works

1

Author sets minlength

Integer character floor.

Markup
2

User types text

Any length allowed.

Input
3

Form validates

Count checked on submit.

Validate
=

Minimum met

Field passes check.

Browser Support

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.

HTML5 · Fully supported

Universal minimum length validation

All major browsers enforce minlength on text inputs and textarea.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
minlength on text inputs & textarea 99% supported

Bottom line: Safe to use on supported elements; validate length on the server too.

💡 Best Practices

✅ Do

  • Set realistic minimums (e.g. 8 for passwords, 3 for usernames)
  • State requirements in labels or help text
  • Pair with maxlength when both bounds matter
  • Use required when the field cannot be empty
  • Validate string length again on the server

❌ Don’t

  • Use minlength on type="number" (use min)
  • Confuse minlength with min
  • Set minimums so high users cannot complete the form
  • Trust client minlength alone for security
  • Mismatch markup values and documentation (e.g. 5 vs 8)

Conclusion

The 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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about minlength

Bookmark these before building text forms.

5
Core concepts
📄 02

text & textarea

Supported elements.

Scope
📈 03

vs maxlength

Min vs max chars.

Compare
⚙️ 04

.minLength

JS number property.

Dynamic
🔒 05

Server too

Client ≠ secure.

Security

❓ Frequently Asked Questions

It sets the minimum number of characters required for a text-like input or textarea to pass validation. The form will not submit until enough characters are entered.
No. minlength 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.
Use 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.
No. Client-side minlength can be bypassed. Always validate string length on the server before storing data.

Require minimum text with minlength

Practice password rules, signup forms, and dynamic minLength in the Try It editor.

Try password example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful