HTML Input Pattern Attribute

HTML Input Patterns: Smarter Fields with Regex

Sometimes, it’s not enough to simply require a field. You want to make sure the data follows a specific format. That’s where the pattern attribute comes in — allowing you to apply regular expressions (regex) directly to your form fields.

What Is the pattern Attribute?

pattern is used in HTML form fields to specify a regular expression that the input must match. If the input doesn’t match the regex, the form will not submit, and the user will see a validation error.

Basic Syntax

<input type="text" name="fruit" pattern="[A-Za-z]+" required>

This pattern allows only letters (uppercase and lowercase) — no numbers, no symbols.

Example: Letters Only (Fruit Name)

<label for="fruit">Enter fruit name:</label><br>
<input type="text" id="fruit" name="fruit" pattern="[A-Za-z]+" required>
Enter fruit name: [ Apple ]

If a user types “Cherry123”, the browser blocks the form submission with a warning.

Example: Numbers Only

Only allow digits 0–9 using pattern="\d+":

<label for="code">Enter numeric code:</label><br>
<input type="text" id="code" name="code" pattern="\d+" required>
Enter numeric code: [ 12345 ]

Example: Starts with "Item" + space + number

<label for="item">Item Format: Item 1, Item 2, etc.</label><br>
<input type="text" id="item" name="item" pattern="Item\s\d+" required>
Item Format: [ Item 1 ]

This field only allows values like “Item 1”, “Item 2”, etc. Spaces and structure matter.

Example: Simple Email Validation

While type="email" handles most cases, here’s how you could apply a basic pattern:

<label for="email">Email:</label><br>
<input type="text" id="email" name="email" 
        pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$" required>
Email: [ hello@example.com ]

This regex checks for basic email structure, but for most uses, type="email" is simpler.

Combining pattern with Placeholder and Title

Give users a hint using placeholder, and add a title to explain what the pattern expects.

<input type="text" name="fruit" 
        pattern="[A-Za-z]+" 
        placeholder="e.g., Apple" 
        title="Only letters allowed" 
        required>
[ e.g., Apple ]

When a user enters an invalid value, the browser displays the message from the title attribute.

Summary of Common Patterns

PatternDescription
[A-Za-z]+Only letters
\d+Only numbers
[a-z]{3,}Lowercase, at least 3 letters
Item\s\d+"Item" followed by space and number
[A-Z][a-z]+Capital first letter, rest lowercase

Full Form Example with Patterns

<form action="/validate" method="post">

  <label for="fruit">Fruit Name (letters only):</label><br>
  <input type="text" id="fruit" name="fruit" pattern="[A-Za-z]+" required><br><br>

  <label for="item">Item Format (Item 1, Item 2):</label><br>
  <input type="text" id="item" name="item" pattern="Item\s\d+" required><br><br>

  <input type="submit" value="Submit">
</form>
Fruit Name: [ Apple ]  
Item Format: [ Item 2 ]  
[Submit]

Best Practices

  • Keep patterns simple for user understanding
  • Always add a title to explain the pattern
  • Test in multiple browsers to ensure consistent validation behavior

Summary

The pattern attribute gives you control over the shape and format of user input — helping ensure your data is clean and meaningful. You’ve learned how to:

  • Apply regex-based validation using pattern
  • Use common patterns like letters-only, numbers-only, and structured strings
  • Combine pattern with required, placeholder, and title for better UX

Check your understanding 1

What is the purpose of the pattern attribute in an HTML input element?
Choose one answer. You can try again.

Answer and explanation

To enforce a specific input format using a regular expression

The pattern attribute uses regular expressions to restrict what users can enter, helping with validation before form submission — for example, enforcing a 6-digit PIN code.

Check your understanding 2

The pattern \d{6} can be used to match exactly six digits, such as an Indian PIN code.
Choose one answer. You can try again.

Answer and explanation

True

Correct. \d matches a digit and {6} specifies exactly six repetitions, which is suitable for values like 500072.

Check your understanding 3

Choose the option that lists all and only the correct statements.

  1. Restricting mobile number to 10 digits
  2. Forcing names to contain only letters
  3. Allowing file uploads
  4. Validating email format without using type="email"
Which of the following are valid use cases for the pattern attribute?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2 and 4

Patterns are often used to enforce numeric limits, restrict characters, or mimic validation (like email) even when not using a specialized input type. File upload requires type="file" and doesn't use pattern.

Check your understanding 4

Which pattern correctly allows only uppercase letters (A–Z) and must be exactly 4 characters long?
Choose one answer. You can try again.

Answer and explanation

<code>[A-Z]{4}</code>

[A-Z]{4} requires four uppercase alphabetic characters only. The others either allow lowercase or an unlimited number of characters.

Check your understanding 5

The pattern attribute can work even if the input type is number.
Choose one answer. You can try again.

Answer and explanation

False

No. The pattern attribute is ignored when type="number" is used. It works with type="text", tel, etc., where input is interpreted as a string.

Check your understanding 6

Choose the option that lists all and only the correct statements.

  1. <input type="text" pattern="\d{10}" required>
  2. <input type="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}">
  3. <input type="number" pattern="\d{4}">
  4. <input type="text" pattern="[A-Za-z ]+">
Which of these HTML inputs correctly use the pattern attribute?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2 and 4

Inputs 0, 1, and 3 use valid pattern attributes. The third one is ignored in browsers because type="number" doesn’t support pattern matching.

What’s Next?

Now that your forms are validated, it’s time to style and polish them with CSS — so users not only interact but enjoy doing so.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: HTML min and max Attribute →