HTML pattern Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Forms & Validation

Introduction

The pattern attribute lets you validate text-like form inputs with a regular expression. The browser checks that the entire value matches your pattern before the form submits (when combined with required or on checkValidity()). Use it for phone formats like XXX-XXX-XXXX, usernames, or postal codes. It works on text, search, tel, url, email, and password — not on number or date. Add a title attribute for a clearer validation message. Always validate again on the server — client-side checks can be bypassed.

What You’ll Learn

01

Regex

Format rules.

02

text / tel

Common types.

03

Full match

Implicit ^ $.

04

title

Hint message.

05

.pattern

JS property.

06

Server too

Never only client.

Purpose of pattern Attribute

The primary purpose of pattern is to enforce a specific text format on form input — so users enter data that matches your rules before submit. Phone numbers, usernames, and ID codes are common use cases where a simple regex catches obvious mistakes early.

The pattern is matched against the whole field value (the browser adds start/end anchors automatically). Pair with placeholder and helper text so users know the expected format. Use type="email" for basic email checks; add pattern only when you need stricter rules.

💡
Client-side only

pattern runs in the browser for convenience. Always re-validate on the server — users can bypass HTML validation with DevTools or custom requests.

📝 Syntax

Add pattern with a regex string on a supported input:

pattern.html
<label for="phone">Phone number:</label>
<input
  type="tel"
  id="phone"
  name="phone"
  pattern="\d{3}-\d{3}-\d{4}"
  placeholder="555-123-4567"
  title="Use format XXX-XXX-XXXX"
  required>

Syntax Rules

  • Value is a regular expression string (no delimiters like /.../).
  • Valid on text, search, tel, url, email, password.
  • Not valid on number, date, checkbox, file, etc.
  • The entire value must match — anchors are implicit.
  • Add title to describe the expected format in validation UI.
  • Often used with required so empty values also fail submit.
  • JavaScript: input.pattern = "[A-Za-z]{3}" or setAttribute("pattern", …).

💎 Values

The pattern attribute accepts any valid regular expression for the field:

  • pattern="\d{3}-\d{3}-\d{4}" — US-style phone 555-123-4567.
  • pattern="[A-Za-z]{3}" — Exactly three letters.
  • pattern="[A-Za-z0-9_]{3,16}" — Username 3–16 alphanumeric/underscore.
  • pattern="[0-9]{5}" — Five-digit postal code.
pattern-examples.html
<!-- Phone on type="tel" (recommended over redundant email regex) -->
<input type="tel" pattern="\d{3}-\d{3}-\d{4}" title="XXX-XXX-XXXX">

<!-- Username -->
<input type="text" pattern="[A-Za-z0-9_]{3,16}" title="3–16 letters, numbers, or _">

⚡ Quick Reference

Use casePatternNotes
Phone XXX-XXX-XXXX\d{3}-\d{3}-\d{4}Use type="tel"
3 letters only[A-Za-z]{3}Exact length
Username[A-Za-z0-9_]{3,16}3–16 chars
Custom messagetitle="…"Shown when invalid
JS updateinput.pattern = "…"Dynamic rules
Check in scriptinput.checkValidity()Returns boolean

Applicable Elements

Input typeSupported?Notes
textYesMost common
telYesPhone formats
email, url, password, searchYesExtra format rules
number, date, timeNoUse min / max instead
checkbox, radio, fileNoNot text-based
<textarea>NoUse minlength / maxlength

pattern vs type="email" vs minlength

AttributeWhat it checksTypical use
patternRegex match on full valuePhone, username, postal code
type="email"Built-in email formatStandard email fields
minlengthMinimum character countPassword length floor
requiredNon-empty valuePair with pattern

Examples Gallery

Phone format validation, dynamic pattern assignment, and username rules with title.

👀 Live Preview

Phone field with pattern="\d{3}-\d{3}-\d{4}" — try an invalid format and click Check:

Enter 555-123-4567 or try 5551234.

Example — Phone number pattern

Require XXX-XXX-XXXX format on a tel input:

phone-pattern.html
<form>
  <label for="phone">Phone number:</label>
  <input
    type="tel"
    id="phone"
    name="phone"
    pattern="\d{3}-\d{3}-\d{4}"
    placeholder="555-123-4567"
    title="Enter phone as XXX-XXX-XXXX"
    required>
  <p>Format: XXX-XXX-XXXX</p>
  <button type="submit">Submit</button>
</form>
Try It Yourself

How It Works

\d{3}-\d{3}-\d{4} means three digits, hyphen, three digits, hyphen, four digits. The whole string must match — you do not need ^ or $.

Dynamic Values with JavaScript

Change the pattern when rules update at runtime:

dynamic-pattern.html
<input type="text" id="dynamicPatternField" required>

<script>
  document.getElementById("dynamicPatternField").pattern = "[A-Za-z]{3}";
</script>
Try It Yourself

How It Works

Assigning input.pattern updates constraint validation immediately. Useful when a dropdown changes which format applies.

Example — Username pattern with title

Allow 3–16 letters, numbers, or underscores:

username-pattern.html
<input
  type="text"
  name="username"
  pattern="[A-Za-z0-9_]{3,16}"
  title="3–16 letters, numbers, or underscores"
  required>
Try It Yourself

How It Works

{3,16} sets length bounds inside the regex. Combine with visible helper text and title for accessibility.

♿ Accessibility

  • Visible instructions — Do not rely on pattern alone; show the expected format in plain text near the field.
  • title attribute — Helps some browsers show why validation failed; not a substitute for a visible label or hint.
  • Associate labels — Every input needs a <label for="…">.
  • Error messaging — Consider setCustomValidity() in JavaScript for clearer messages than raw regex.
  • Do not block assistive tech — Invalid state is announced; keep hints concise and actionable.

🧠 How pattern Works

1

Author sets regex

pattern on input.

Markup
2

User types value

Full string.

Input
3

Browser matches

Implicit ^ $.

Validate
=

Submit or block

Valid / invalid.

Browser Support

The pattern attribute is part of HTML5 constraint validation and is supported in all modern browsers.

HTML5 Forms · Fully supported

Universal pattern validation

Regex constraint checks work in every major browser.

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
Internet Explorer IE 10+ supported
Full support
Opera Fully supported
Full support
pattern attribute 99% supported

Bottom line: Safe for client-side format hints in all modern browsers — always validate on the server too.

💡 Best Practices

✅ Do

  • Use pattern on text / tel for custom formats
  • Add title and visible format hints
  • Keep regexes as simple as possible
  • Pair with required when the field must not be empty
  • Re-validate every submission on the server

❌ Don’t

  • Rely on pattern alone for security
  • Put complex email regex on type="email" when built-in validation suffices
  • Use pattern on number or textarea
  • Forget ^/$ are implicit — design for full-string match
  • Hide format rules — users need clear instructions

Conclusion

The pattern attribute enforces a regular-expression format on text-like inputs — a simple way to catch wrong phone numbers, usernames, or codes before submit.

Use clear hints and title, prefer type="email" for basic emails, and always validate again on the server.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about pattern

Bookmark these before adding regex validation.

5
Core concepts
📱 02

text / tel

Supported.

Scope
💬 03

title

User hint.

UX
04

.pattern JS

Dynamic.

API
05

Server too

Required.

Security

❓ Frequently Asked Questions

It holds a regular expression. The browser uses it for constraint validation — the entire input value must match the pattern for the field to be considered valid.
text, search, tel, url, email, and password. Not number, date, or textarea.
No. HTML implicitly anchors the regex to the full value — equivalent to wrapping your pattern with ^(?:…)$.
Prefer type="email" for standard email fields. The old reference’s long email regex on type="email" is usually redundant — use pattern only for stricter custom rules.
input.pattern = "[A-Za-z]{3}" or setAttribute("pattern", "…"). Call checkValidity() to test the current value.
Yes in all modern browsers with HTML5 form validation (IE 10+).

Validate form formats

Practice the pattern attribute with phone, username, and dynamic regex examples in the Try It editor.

Try pattern demo →

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