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

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.
Format rules.
Common types.
Implicit ^ $.
Hint message.
JS property.
Never only client.
pattern AttributeThe 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.
pattern runs in the browser for convenience. Always re-validate on the server — users can bypass HTML validation with DevTools or custom requests.
Add pattern with a regex string on a supported input:
<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>/.../).text, search, tel, url, email, password.number, date, checkbox, file, etc.title to describe the expected format in validation UI.required so empty values also fail submit.input.pattern = "[A-Za-z]{3}" or setAttribute("pattern", …).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.<!-- 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 _">| Use case | Pattern | Notes |
|---|---|---|
| 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 message | title="…" | Shown when invalid |
| JS update | input.pattern = "…" | Dynamic rules |
| Check in script | input.checkValidity() | Returns boolean |
| Input type | Supported? | Notes |
|---|---|---|
text | Yes | Most common |
tel | Yes | Phone formats |
email, url, password, search | Yes | Extra format rules |
number, date, time | No | Use min / max instead |
checkbox, radio, file | No | Not text-based |
<textarea> | No | Use minlength / maxlength |
pattern vs type="email" vs minlength| Attribute | What it checks | Typical use |
|---|---|---|
pattern | Regex match on full value | Phone, username, postal code |
type="email" | Built-in email format | Standard email fields |
minlength | Minimum character count | Password length floor |
required | Non-empty value | Pair with pattern |
Phone format validation, dynamic pattern assignment, and username rules with title.
Phone field with pattern="\d{3}-\d{3}-\d{4}" — try an invalid format and click Check:
Enter 555-123-4567 or try 5551234.
Require XXX-XXX-XXXX format on a tel input:
<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>\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 $.
Change the pattern when rules update at runtime:
<input type="text" id="dynamicPatternField" required>
<script>
document.getElementById("dynamicPatternField").pattern = "[A-Za-z]{3}";
</script>Assigning input.pattern updates constraint validation immediately. Useful when a dropdown changes which format applies.
Allow 3–16 letters, numbers, or underscores:
<input
type="text"
name="username"
pattern="[A-Za-z0-9_]{3,16}"
title="3–16 letters, numbers, or underscores"
required>{3,16} sets length bounds inside the regex. Combine with visible helper text and title for accessibility.
pattern alone; show the expected format in plain text near the field.<label for="…">.setCustomValidity() in JavaScript for clearer messages than raw regex.pattern on input.
Full string.
Implicit ^ $.
Valid / invalid.
The pattern attribute is part of HTML5 constraint validation and is supported in all modern browsers.
pattern validationRegex constraint checks work in every major browser.
Bottom line: Safe for client-side format hints in all modern browsers — always validate on the server too.
pattern on text / tel for custom formatstitle and visible format hintsrequired when the field must not be emptypattern alone for securitytype="email" when built-in validation sufficespattern on number or textarea^/$ are implicit — design for full-string matchThe 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.
patternBookmark these before adding regex validation.
Full match.
SyntaxSupported.
ScopeUser hint.
UXDynamic.
APIRequired.
Securitytext, search, tel, url, email, and password. Not number, date, or textarea.^(?:…)$.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.Practice the pattern attribute with phone, username, and dynamic regex examples in the Try It editor.
5 people found this page helpful