Table of contents
1.
Introduction
2.
Definition and Usage  
2.1.
Example: Using HTML Pattern for a Phone Number  
3.
Syntax
4.
Element
5.
Applies to  
5.1.
1. Text Input  
5.2.
2. Email Input  
5.3.
3. Password Input  
5.4.
4. Other Input Types  
6.
Attribute
6.1.
Commonly used attributes with pattern:
6.2.
Example
7.
Supported Browsers
8.
Frequently Asked Questions
8.1.
What happens if an input does not match the pattern?
8.2.
Can the pattern attribute be used with all input types?
8.3.
How can I create a custom validation message?
9.
Conclusion
Last Updated: Feb 24, 2025
Medium

What is Pattern Attribute in HTML?

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The pattern attribute in HTML is used in <input> fields to define a regular expression that the input value must match. It helps in validating user input, ensuring that data follows a specific format, such as email addresses, phone numbers, or passwords. If the input does not match the pattern, the form will not submit. 

What is Pattern Attribute in HTML?

In this article, you will learn how to use the pattern attribute effectively with examples.

Definition and Usage  

HTML patterns are created using the `pattern` attribute in HTML. This attribute is used with input fields like text boxes to define a regular expression. A regular expression, or regex, is a sequence of characters that forms a search pattern. It helps validate the format of the data entered by the user. For example, if you want users to enter a valid phone number, you can use a pattern to ensure they only type numbers & follow a specific format.

The `pattern` attribute works with input types like `text`, `email`, `password`, & more. When a user tries to submit a form, the browser checks if the input matches the pattern. If it doesn’t, the form won’t submit, & the user will see an error message. This ensures the data collected is clean & consistent.

Example: Using HTML Pattern for a Phone Number  

Let’s say you want users to enter a 10-digit phone number. Here’s how you can use the `pattern` attribute to enforce this rule:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HTML Pattern Example</title>
</head>
<body>
    <form action="/submit" method="post">
        <label for="phone">Enter your 10-digit phone number:</label><br>
        <input type="text" id="phone" name="phone" pattern="\d{10}" required>
        <small>Format: 1234567890</small><br><br>
        <button type="submit">Submit</button>
    </form>
</body>
</html>

 

Output

Output

 

In this Code:   

1. `<input>` Element: The `type="text"` specifies that the input field accepts text. The `pattern="\d{10}"` ensures that only 10 digits are allowed. Here, `\d` means any digit, & `{10}` specifies exactly 10 occurrences.
 

2. `required` Attribute: This makes sure the field cannot be left empty.
 

3. Error Handling: If the user enters anything other than 10 digits, the browser shows an error when they try to submit the form.
 

This approach helps developers control user input without needing complex JavaScript validation. It’s simple, effective, & improves the overall quality of data collected.

Syntax

The pattern attribute is used inside an <input> tag to define a regular expression that the entered value must match.

<input type="text" pattern="[A-Za-z]+" title="Only letters are allowed">

 

Explanation:

  • The pattern attribute ensures that only letters (A-Z, a-z) are accepted.
     
  • The title attribute provides a message when the input does not match the pattern.

Element

The pattern attribute is mostly used with the <input> element in HTML forms. It is useful for validating different types of data such as phone numbers, email addresses, and passwords.

<!DOCTYPE html>
<html lang="en">
<head>
 
</head>
<body>
  <form>
    <label for="username">Username:</label>
    <input type="text" id="username" name="username" pattern="[A-Za-z0-9]{5,12}" title="5 to 12 alphanumeric characters only" required>
    <input type="submit" value="Submit">
</form>

</body>
</html>

 

Output

Output

 

Explanation:

  • The pattern allows only letters and numbers.
     
  • The length must be between 5 to 12 characters.
     
  • The required attribute ensures the field is not left empty.

Applies to  

The `pattern` attribute in HTML applies to specific input types where text-based validation is needed. It’s not universal & works only with certain input fields. Understanding where you can use this attribute is important to make the most of it in your projects. Now, let’s discuss the input types that support the `pattern` attribute with proper examples: 

1. Text Input  

The `text` input type is the most common field where patterns are applied. It’s used for general text like names, addresses, or custom codes.  

Example: Validating a Username  

Let’s say you want users to create a username that is 5 to 10 characters long & contains only letters or numbers. Let’s see how you can do it:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Username Validation</title>
</head>
<body>
    <form action="/submit" method="post">
        <label for="username">Create a username (5-10 characters, letters & numbers only):</label><br>
        <input type="text" id="username" name="username" pattern="[A-Za-z0-9]{5,10}" required>
        <small>Format: abc123</small><br><br>
        <button type="submit">Submit</button>
    </form>
</body>
</html>

 

Output

Output

In this code:  

  • The `pattern="[A-Za-z0-9]{5,10}"` ensures the username contains only uppercase (`A-Z`), lowercase (`a-z`), or numeric (`0-9`) characters.  
     
  • `{5,10}` specifies the length should be between 5 & 10 characters.  

2. Email Input  

The `email` input type already validates email formats, but you can use the `pattern` attribute to add extra rules, like restricting domain names.  

Example: Restricting Email Domains  

If you want users to enter an email address from a specific domain, like `.edu`, here’s how:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Email Domain Validation</title>
</head>
<body>
    <form action="/submit" method="post">
        <label for="email">Enter your .edu email address:</label><br>
        <input type="email" id="email" name="email" pattern="^[a-zA-Z0-9._%+-]+@([a-zA-Z0-9-]+\.)+edu$" required>
        <small>Format: example@university.edu</small><br><br>
        <button type="submit">Submit</button>
    </form>
</body>
</html>

 

Output

Output

 

In this code:  

  • The regex `^[a-zA-Z0-9._%+-]+@([a-zA-Z0-9-]+\.)+edu$` ensures the email ends with `.edu`. 
     
  • This is useful for websites targeting students or educational institutions.  

3. Password Input  

For password fields, you can enforce rules like minimum length, inclusion of special characters, or a mix of uppercase & lowercase letters.  

Example: Strong Password Validation  

Let’s discuss an example of ensuring passwords are at least 8 characters long & include at least one number & one uppercase letter:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Password Validation</title>
</head>
<body>
    <form action="/submit" method="post">
        <label for="password">Create a strong password (minimum 8 characters, 1 uppercase, 1 number):</label><br>
        <input type="password" id="password" name="password" pattern="^(?=.[A-Z])(?=.\d).{8,}$" required>
        <small>Format: Example123</small><br><br>
        <button type="submit">Submit</button>
    </form>
</body>
</html>

 

Output

Output

 

Explanation:  

  • `(?=.[A-Z])` ensures there’s at least one uppercase letter.  
     
  • `(?=.\d)` ensures there’s at least one number.  
     
  • `.{8,}` specifies the password must be at least 8 characters long.  

4. Other Input Types  

The `pattern` attribute also works with input types like `search`, `tel`, & `url`. However, it doesn’t apply to inputs like `checkbox`, `radio`, `range`, or `color`. Always check compatibility before using it.  

Attribute

The pattern attribute works alongside other attributes to improve validation.

Commonly used attributes with pattern:

  1. type – Defines the input type (e.g., text, email, password).
     
  2. title – Provides a message when the pattern is not matched.
     
  3. required – Ensures the field is not left empty.
     
  4. minlength & maxlength – Defines length constraints.

Example

<input type="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" title="Enter a valid email address">

 

Explanation:

  • Ensures that the user enters a valid email format.

Example

Below is a practical example that validates a password with specific requirements.

<!DOCTYPE html>
<html lang="en">
<head>
 
</head>
<body>
  <form>
 <form>
    <label for="password">Password:</label>
    <input type="password" id="password" name="password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" title="Must contain at least one number, one uppercase, one lowercase letter, and at least 8 characters" required>
    <input type="submit" value="Submit">
</form>
</body>
</html>


Output

Output

 

Explanation

  • The password must have at least one uppercase letter, one lowercase letter, one number, and be at least 8 characters long.

Supported Browsers

The pattern attribute is widely supported by modern browsers:

BrowserSupport
Google ChromeYes
Mozilla FirefoxYes
Microsoft EdgeYes
SafariYes
OperaYes

Frequently Asked Questions

What happens if an input does not match the pattern?

If the input value does not match the pattern, the form will not submit, and the browser will show an error message.

Can the pattern attribute be used with all input types?

No, it is mainly used with text-based input types like text, email, password, and tel.

How can I create a custom validation message?

You can use the title attribute to display a custom message when validation fails.

Conclusion

In this article, we learned the pattern attribute in HTML, which is used in <input> fields to define a regular expression for input validation. It ensures users enter data in a specific format, such as phone numbers, email addresses, or passwords. Understanding the pattern attribute helps improve form validation, user experience, and data accuracy in web development.

Live masterclass