/
Ever wondered how websites check if you entered your phone number correctly or if your password meets their requirements? The secret lies in HTML's pattern attribute and something called "regex" - your new best friends for making sure users enter the right information.
In this beginner-friendly guide, you'll learn how to use the pattern attribute to validate form inputs automatically. No complicated programming needed - just simple HTML that makes your forms smarter and more user-friendly.
The pattern attribute is like a quality checker for your HTML forms. It tells the browser exactly what kind of information you expect users to type in your input fields. Think of it as a template that says "the input should look like this."
When users try to submit a form, the browser automatically checks if their input matches your pattern. If it doesn't match, the browser shows an error message and won't let them submit the form until they fix it.
This attribute works with text, email, password, search, tel, and url input types, making it incredibly versatile for different kinds of data validation.
Regex (short for Regular Expression) is a special language that describes text patterns. It might sound scary, but think of it as a way to describe what valid information should look like using simple symbols and rules.
For example, a regex pattern for a phone number might say "start with 3 digits, then a dash, then 3 more digits, then another dash, then 4 final digits" which would match phone numbers like 123-456-7890.
Regex is used inside the pattern attribute to define exactly what kind of input you want to accept. Don't worry - you don't need to become a regex expert to use common patterns!
When you use the pattern attribute, browsers automatically:
Unlike complex validation scripts, pattern validation works with pure HTML. This means:
You can provide helpful error messages that explain what users need to fix:
<input type="text" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
title="Please enter phone number as 123-456-7890">The pattern attribute goes inside your input tag:
<input type="text" name="field" pattern="your-pattern-here">Here are some easy patterns to get you started:
<!-- Only letters (a-z, A-Z) -->
<input type="text" pattern="[a-zA-Z]+"
placeholder="Enter only letters">
<!-- Only numbers -->
<input type="text" pattern="[0-9]+"
placeholder="Enter only numbers">
<!-- Exactly 5 digits -->
<input type="text" pattern="[0-9]{5}"
placeholder="Enter exactly 5 numbers">Always include helpful labels and title attributes:
<label for="zipcode">ZIP Code:</label>
<input type="text" id="zipcode" name="zipcode"
pattern="[0-9]{5}"
title="Please enter a 5-digit ZIP code"
placeholder="12345">Create a simple phone number validator:
<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="Please use format: 123-456-7890"
placeholder="123-456-7890" required>
<input type="submit" value="Submit">
</form>What this does:
Validate US ZIP codes (5 digits):
<form>
<label for="zip">ZIP Code:</label>
<input type="text" id="zip" name="zip"
pattern="[0-9]{5}"
title="Please enter a 5-digit ZIP code"
placeholder="12345" required>
<input type="submit" value="Submit">
</form>What this does:
Create rules for usernames:
<form>
<label for="username">Username:</label>
<input type="text" id="username" name="username"
pattern="[a-zA-Z0-9]{3,15}"
title="Username must be 3-15 characters, letters and numbers only"
placeholder="Enter username" required>
<input type="submit" value="Create Account">
</form>What this does:
Basic password validation:
<form>
<label for="password">Password:</label>
<input type="password" id="password" name="password"
pattern=".{8,}"
title="Password must be at least 8 characters long"
placeholder="Enter password" required>
<input type="submit" value="Sign Up">
</form>What this does:
Pattern validation is perfect for:
You'll use patterns in:
Patterns help businesses by:
Pattern validation improves user experience by:
For developers, patterns offer:
Your website gets:
While pattern validation works in all modern browsers:
Keep patterns simple because:
Pattern validation has some limits:
Start with basic patterns and add complexity only when needed:
<!-- Good: Simple and clear -->
<input type="text" pattern="[0-9]{5}"
title="Enter 5 digits for ZIP code">
<!-- Avoid: Too complex for beginners -->
<input type="text" pattern="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$">Always tell users what format you expect:
<label for="phone">Phone (123-456-7890):</label>
<input type="tel" id="phone" name="phone"
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
title="Please enter phone as 123-456-7890"
placeholder="123-456-7890">Always test your validation by:
Use pattern validation alongside other HTML5 validation:
<form>
<label for="email">Email:</label>
<input type="email" id="email" name="email"
pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$"
title="Please enter a valid email address"
required>
<label for="age">Age:</label>
<input type="number" id="age" name="age"
min="18" max="120" required>
<input type="submit" value="Submit">
</form>Here are some patterns you'll use frequently:
<!-- Letters only -->
<input type="text" pattern="[a-zA-Z]+" title="Letters only">
<!-- Numbers only -->
<input type="text" pattern="[0-9]+" title="Numbers only">
<!-- Letters and numbers -->
<input type="text" pattern="[a-zA-Z0-9]+" title="Letters and numbers only">
<!-- Exact length (5 characters) -->
<input type="text" pattern=".{5}" title="Exactly 5 characters">
<!-- Minimum length (8 characters) -->
<input type="text" pattern=".{8,}" title="At least 8 characters">
<!-- Range (3 to 10 characters) -->
<input type="text" pattern=".{3,10}" title="Between 3 and 10 characters"><!-- Credit card (simplified) -->
<input type="text" pattern="[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}"
title="Enter as 1234-5678-9012-3456">
<!-- Social Security Number -->
<input type="text" pattern="[0-9]{3}-[0-9]{2}-[0-9]{4}"
title="Enter as 123-45-6789">
<!-- Simple email pattern -->
<input type="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}"
title="Enter a valid email address">Congratulations! You now understand how to use HTML's pattern attribute to validate form inputs automatically. This powerful feature helps you create professional, user-friendly forms that guide users to enter information correctly.
Remember the key points: use simple patterns that match your data requirements, always provide clear instructions and error messages, and test your forms thoroughly. Start with basic patterns like phone numbers and ZIP codes, then gradually work your way up to more complex validation as you gain experience.
Pattern validation is one of the most useful HTML skills you can learn. It makes your forms smarter, your users happier, and your data cleaner - all with just a few simple HTML attributes. Start practicing with the examples in this guide, and you'll be creating professional-quality forms in no time!