splitforms.com

HTML · INPUT ATTRIBUTE

HTML minlength: require a minimum text length

minlength sets the fewest characters a text-like <input> or <textarea> must hold. When a visitor types a value that is too short, the browser blocks the submit and flags validity.tooShort. An empty field still passes unless it also has required, so always check the length on the server too.

  • Live, editable example
  • Copy-paste snippet
  • Attributes & common mistakes
Live demo · nothing is sent

What gets submitted

Fill in the form above and press its button. The demo stops the request and lists every name=value pair the browser would send, plus the controls it leaves out.

With a free access key in the form, these fields land in your splitforms dashboard; access_key and botcheck are read by splitforms, not stored as lead data.

Edit the HTML and re-run it
Runs in a sandbox. Nothing is sent.

Minimal snippet

minlength-example.html
<input type="text" name="username" minlength="3" maxlength="20" required>

Want this form to email you its submissions? Point it at a free splitforms endpoint, with no server to build. Get a free access key →

Copy-paste code

HTML minlength attribute code you can paste today.

Replace YOUR_ACCESS_KEY with a key from your free splitforms dashboard. The form posts to one endpoint — submissions land in your inbox and dashboard with spam filtering built in.

minlength.html
<form action="https://splitforms.com/api/submit" method="POST">
  <input type="hidden" name="access_key" value="YOUR_ACCESS_KEY">

  <label for="email">Email</label>
  <input type="email" id="email" name="email" autocomplete="email" required>

  <label for="message">Message</label>
  <textarea id="message" name="message" rows="5" minlength="20" required></textarea>

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

  <!-- Honeypot: hidden from people, bots fill it in -->
  <input type="checkbox" name="botcheck" style="display:none">

  <button type="submit">Send</button>
</form>

Attributes · minlength

Which HTML minlength attribute attributes matter?

These attributes decide how the minlength behaves and what actually gets emailed to your inbox when someone submits.

HTML minlength attribute attributes and what each one does
AttributeWhat it doesDocs
minlengthThe fewest characters a non-empty value may have, like minlength="8". A shorter edited value blocks the submit.MDN
maxlengthThe most characters the field accepts. Typing stops at the limit.MDN
requiredMakes the field mandatory. Pair it with minlength so an empty field can't slip through.MDN
patternA regular expression the whole value must match, for rules about which characters are allowed.MDN

Common mistakes

HTML minlength attribute not working? Here's why.

Most broken forms come down to a few small mistakes with the minlength. Check these before you ship, then forward every submission to Slack or a webhook.

  1. An empty field passes minlength

    minlength only checks a value that isn't empty. Add required if the field must be filled in.

  2. Only user edits are checked

    The check runs after the user edits the field. A short value set by script or by the value attribute is not flagged as tooShort.

  3. Emoji and some characters count as 2

    Length is counted in UTF-16 code units, so an emoji like 😀 counts as two characters.

  4. It does nothing on number, date or range inputs

    Those types ignore minlength. Use min to set the lowest allowed value, and always check the length again on the server, because anyone can remove the attribute.

Sources: WHATWG HTML Standard: minlength · MDN: minlength

Key benefits

Copy-paste ready

The HTML minlength attribute example below is a complete, working form — paste it into any HTML page and it submits, no edits to the markup required.

No backend to build

Your html minlength attribute form posts to one endpoint. No server, PHP, or API route — accepted submissions are saved to your dashboard before owner notification email is attempted; delivery time and inbox placement depend on provider and recipient settings.

A form that really submits

Unlike a static reference snippet, this minlength form actually sends data to a free splitforms backend, with spam filtering built in.

How it works

How to add HTML minlength attribute to any page.

Copy the html minlength attribute example, drop in your free access key, and paste it into any page — submissions start landing in your dashboard.

  1. Copy the code

    Copy the HTML minlength attribute example below.

  2. Add your access key

    Replace YOUR_ACCESS_KEY with a free splitforms key from the dashboard.

  3. Paste into your site

    Drop the form into any HTML page. Submissions arrive in your dashboard.

Questions

HTML minlength attribute questions, answered.

Anything missing? Email us, we read every message.

A person reading answers to common questions
Why isn't minlength working?

Two common reasons. An empty field always passes, so add required if the field must be filled in. And the browser only checks a value the user has edited, so a short value set by script or by the value attribute is not flagged.

What is the difference between minlength and pattern?

minlength only counts characters. pattern checks the whole value against a regular expression, so it can control which characters are allowed. You can use both on the same field.

How do I set a minimum length for a textarea?

Add minlength to the <textarea>, for example minlength="20", and add required if it must not be empty. The browser blocks the submit while the edited text is shorter.

How do I show a custom minlength error message?

The browser shows its own message by default. To change it, listen for the field's input event and call field.setCustomValidity("Please enter at least 20 characters.") when field.validity.tooShort is true, then call setCustomValidity("") once the length is valid again so the form can submit. You can also listen for the invalid event to set the message just before the browser shows it.

Does minlength work on type="number"?

No. Number inputs ignore minlength. Use min to set the lowest allowed value, or a text input with inputmode="numeric" if you need a minimum number of digits.

View all FAQs

Make this form actually submit.

Point your form at splitforms. 500 free submissions a month, spam filtering, webhooks on paid plans — no server.

  • No credit card required
  • 500 free submissions
  • Setup in minutes
Two people celebrating a working contact form