HTML pattern Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The pattern attribute sets a regular expression that a text-like <input> value should match. If the value is not empty and does not match, constraint validation fails (ValidityState.patternMismatch is true).

Supported types: text, search, tel, url, email, and password. Related: required, title (hint for the pattern), minlength / maxlength, and oninvalid.

Baseline · Widely available.

Not deprecated and not experimental. Per MDN, the regex must match the entire value (implied full-string match). No / delimiters around the pattern. Stable since about July 2015.

Client-side only.

Users can bypass browser checks. Always validate the same rules on the server. Also put the format in visible text near the field — do not rely on title alone for accessibility.

HTML Syntax

syntax.html
<input pattern=" regexp">

regexp is a JavaScript regular expression without surrounding slashes. Invalid or empty pattern values are ignored (empty pattern with a non-empty value is treated specially — see Edge Cases).

Attribute Values

The value is a regex string. Common study patterns:

Value / patternDescriptionTry it
[a-z]{4,8}4–8 lowercase letters (username-style)Try it
\d{4}Exactly four digits (PIN-style)Try it
[A-Za-z0-9_]{3,16}Letters, digits, underscore; length 3–16Try it
+ requiredEmpty is allowed by pattern alone — require a value tooTry it

Live Preview

Type a username ([a-z]{4,8}) and submit. Status shows whether constraint validation passes.

Demo

Visible hint text explains the format. Submit is canceled so you stay on this page.

Preview

4–8 lowercase letters only.

Waiting…

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Lowercase Username

Restrict to 4–8 lowercase letters, with visible help and a title.

HTML
<form id="f">
  <label for="uname">Username</label>
  <input id="uname" name="uname" type="text" required
         pattern="[a-z]{4,8}"
         title="4 to 8 lowercase letters">
  <p>Use 4–8 lowercase letters (a–z).</p>
  <button type="submit">Submit</button>
</form>

<script>
  document.getElementById("f").addEventListener("submit", function (e) {
    e.preventDefault();
    alert("Valid username.");
  });
</script>
Try It Yourself

How It Works

  1. pattern="[a-z]{4,8}" allows only lowercase a–z, length 4–8.
  2. Full-value match — hello! fails even if it contains letters.
  3. required rejects an empty field.
  4. Visible text + title explain the rule (MDN usability guidance).

Example 2 — Four-Digit PIN

Use digits only with \d{4} on a password field.

HTML
<form id="f">
  <label for="pin">PIN</label>
  <input id="pin" name="pin" type="password" inputmode="numeric"
         pattern="\d{4}" required
         title="Exactly 4 digits">
  <p>Enter exactly 4 digits.</p>
  <button type="submit">Submit</button>
</form>

<script>
  document.getElementById("f").addEventListener("submit", function (e) {
    e.preventDefault();
    alert("PIN format OK.");
  });
</script>
Try It Yourself

How It Works

  1. \d{4} means exactly four digits (0–9).
  2. type="password" still supports pattern.
  3. inputmode="numeric" helps mobile keyboards (optional).
  4. Submit is blocked by the browser until the value matches.

Example 3 — Handle with Letters, Digits, Underscore

A looser handle: 3–16 “word” characters.

HTML
<form id="f">
  <label for="handle">Handle</label>
  <input id="handle" name="handle" type="text" required
         pattern="[A-Za-z0-9_]{3,16}"
         title="3 to 16 letters, digits, or underscore">
  <p>Letters, digits, and _ only (3–16 characters).</p>
  <button type="submit">Submit</button>
</form>

<script>
  document.getElementById("f").addEventListener("submit", function (e) {
    e.preventDefault();
    alert("Handle looks valid.");
  });
</script>
Try It Yourself

How It Works

  1. Character class lists allowed characters explicitly.
  2. {3,16} sets min and max length in the same regex.
  3. Spaces and symbols outside the class fail the match.
  4. Prefer type="email" for emails instead of inventing an email regex.

Edge Cases & Pitfalls

Most “pattern never works” bugs are wrong input type, missing required, or wrapping the regex in /.

types

Only certain input types

Works on text, search, tel, url, email, password. On number or date, use the constraints those types already provide — not pattern.

empty

Empty value skips pattern

An empty string does not trigger patternMismatch. Add required when the field must be filled.

slashes

No /regex/ wrappers

Write pattern="\d{4}", not pattern="/\d{4}/". Forward slashes are not part of the HTML attribute value.

a11y

Visible format text

MDN: describe the pattern in visible text near the control. title may appear in tooltips/errors but is not enough alone for accessibility.

server

Not a security boundary

Constraint validation is optional UX. Re-check formats on the server every time.

Browser Support

pattern is an HTML attribute for constraint validation on text-like inputs in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

pattern

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support pattern with HTML5 constraint validation.

97% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer 10+
Supported
pattern Widely supported

Bottom line: Safe to use for UX validation on supported input types. Always pair with visible hints and server-side checks.

Key Takeaways

  • Role: regex must match the whole non-empty value.
  • Types: text, search, tel, url, email, password.
  • Add required when empty should also fail.
  • Explain the format in visible text; validate on the server too.

One line: pattern is a full-string regex for text-like inputs — teach the format on the page, then check again on the server.

Frequently Asked Questions

On supported text-like inputs, pattern is a regular expression. For constraint validation, a non-empty value must match the entire pattern or ValidityState.patternMismatch is true.
text, search, tel, url, email, and password. It does not apply to number, date, checkbox, radio, file, and similar types.
No. Per MDN, the browser matches the entire value — as if the pattern were wrapped with implied start/end anchors. Do not wrap the pattern in forward slashes.
An empty string does not fail pattern by itself. Combine pattern with required if the field must be filled and match the regex.
Prefer type=email for normal email syntax. Use pattern when you need a custom format (for example a company domain) on text, tel, or other supported types.
No. It is Baseline widely available since about July 2015. Client-side pattern checks are a UX aid — always validate again on the server.

Next: placeholder

Show hint text inside an empty form control.

placeholder tutorial →

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.

8 people found this page helpful