The pattern attribute provides powerful validation using regular expressions to enforce specific formats for text input. Understanding pattern validation is essential for creating forms with custom validation requirements. The pattern attribute accepts a JavaScript regular expression that the input value must match. When users submit the form, the browser checks if the input matches the pattern. If it does not match, the browser prevents submission and displays an error message. Pattern validation only works with text-based input types including text, tel, email, url, password, and search. It is ignored for other input types. The pattern is applied to the entire value, so you do not need to add anchors like caret or dollar sign. Basic pattern examples include digits only with pattern equals square bracket 0 hyphen 9 close bracket plus, which allows one or more digits. Letters only uses pattern equals square bracket A hyphen Z a hyphen z close bracket plus for one or more letters. Alphanumeric uses pattern equals square bracket A hyphen Z a hyphen z 0 hyphen 9 close bracket plus. Exact length uses pattern equals square bracket 0 hyphen 9 close bracket curly brace 5 close curly brace for exactly five digits. Range uses pattern equals square bracket 0 hyphen 9 close bracket curly brace 3 comma 6 close curly brace for three to six digits. Common practical patterns include US phone numbers with pattern equals square bracket 0 hyphen 9 close bracket curly brace 3 close curly brace hyphen square bracket 0 hyphen 9 close bracket curly brace 3 close curly brace hyphen square bracket 0 hyphen 9 close bracket curly brace 4 close curly brace for format 123-456-7890. ZIP codes use pattern equals square bracket 0 hyphen 9 close bracket curly brace 5 close curly brace for five-digit codes. Credit cards can use pattern equals square bracket 0 hyphen 9 close bracket curly brace 16 close curly brace for 16 digits. Usernames might use pattern equals square bracket A hyphen Z a hyphen z 0 hyphen 9 underscore close bracket curly brace 3 comma 15 close curly brace for alphanumeric with underscores, three to fifteen characters. Hexadecimal colors use pattern equals hashtag square bracket 0 hyphen 9 A hyphen F close bracket curly brace 6 close curly brace. The title attribute provides crucial user guidance. When validation fails, some browsers show the title text in the error message. Always include a title that explains the required format in plain language. For example, title equals Format colon 123 hyphen 456 hyphen 7890. Advanced pattern features include character classes, where square bracket abc close bracket matches a, b, or c. Square bracket caret abc close bracket matches anything except a, b, or c. Square bracket a hyphen z close bracket matches any lowercase letter. Quantifiers include asterisk for zero or more, plus for one or more, question mark for zero or one, and curly brace n close curly brace for exactly n. Curly brace n comma close curly brace means n or more. Curly brace n comma m close curly brace means between n and m. Grouping uses parentheses like parenthesis abc close parenthesis plus matches abc, abcabc, etc. Alternation uses pipe like cat pipe dog matches cat or dog. Escape special characters with backslash like backslash dot matches a literal period. Best practices include keeping patterns as simple as possible for user understanding. Use title to explain the format requirement clearly. Test patterns with valid and invalid inputs before deployment. Remember that pattern is case-sensitive unless you use both uppercase and lowercase in character classes. Combine pattern with other attributes like minlength, maxlength, and required for comprehensive validation. Always validate on the server as pattern can be bypassed. Consider providing real-time feedback with JavaScript for better user experience. Limitations include pattern validation being client-side only and bypassable. Regular expressions can be complex and hard to maintain. Some users may not understand error messages. Browser support varies for complex regex features. Pattern cannot validate against server data. Understanding pattern validation demonstrates advanced knowledge of form validation and regular expressions, topics frequently tested in technical interviews. Many companies ask candidates to write patterns for common formats or debug incorrect patterns.
Example code
<!-- US PHONE NUMBER -->
<form>
<label for="phone">Phone Number:</label>
<input type="tel"
id="phone"
name="phone"
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
title="Format: 123-456-7890"
placeholder="123-456-7890"
required>
<button type="submit">Submit</button>
</form>
<!-- USERNAME (alphanumeric + underscore, 3-15 chars) -->
<form>
<label for="username">Username:</label>
<input type="text"
id="username"
name="username"
pattern="[A-Za-z0-9_]{3,15}"
title="3-15 characters: letters, numbers, underscore only"
required>
</form>
<!-- ZIP CODE (5 or 9 digits) -->
<form>
<label for="zip">ZIP Code:</label>
<input type="text"
id="zip"
name="zip"
pattern="[0-9]{5}(-[0-9]{4})?"
title="5 digits or 5+4 format (12345 or 12345-6789)"
placeholder="12345 or 12345-6789"
required>
</form>
<!-- PASSWORD (min 8 chars, 1 uppercase, 1 lowercase, 1 number) -->
<form>
<label for="password">Password:</label>
<input type="password"
id="password"
name="password"
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}"
title="At least 8 characters with 1 uppercase, 1 lowercase, and 1 number"
minlength="8"
required>
</form>
<!-- EMAIL (custom pattern) -->
<form>
<label for="email">Work Email:</label>
<input type="text"
id="email"
name="email"
pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$"
title="Valid email address"
required>
</form>
<!-- CREDIT CARD (16 digits) -->
<form>
<label for="cc">Credit Card:</label>
<input type="text"
id="cc"
name="card"
pattern="[0-9]{16}"
title="16-digit card number"
placeholder="1234567890123456"
maxlength="16"
required>
</form>
<!-- PRODUCT CODE (format: ABC-123) -->
<form>
<label for="code">Product Code:</label>
<input type="text"
id="code"
name="product_code"
pattern="[A-Z]{3}-[0-9]{3}"
title="Format: ABC-123 (3 uppercase letters, hyphen, 3 digits)"
placeholder="ABC-123"
required>
</form>
<!-- HEX COLOR -->
<form>
<label for="color">Color Code:</label>
<input type="text"
id="color"
name="color"
pattern="#[0-9A-Fa-f]{6}"
title="Hex color code (e.g., #FF5733)"
placeholder="#FF5733"
maxlength="7"
required>
</form>
<!-- COMPREHENSIVE REGISTRATION FORM -->
<form action="/register" method="POST">
<!-- Username pattern -->
<input type="text"
name="username"
pattern="[A-Za-z0-9_]{3,15}"
title="3-15 characters"
required>
<!-- Email (using type=email + pattern) -->
<input type="email"
name="email"
pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$"
title="Valid email"
required>
<!-- Phone pattern -->
<input type="tel"
name="phone"
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
title="123-456-7890"
required>
<!-- Strong password pattern -->
<input type="password"
name="password"
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*]).{8,}"
title="Min 8 chars with uppercase, lowercase, number, special char"
required>
<button type="submit">Register</button>
</form>