HTML: input pattern

The pattern attribute validates an input value against a regular expression when the form is submitted.

What you will learn

Phone number example

<label for="phone">Phone number</label>
<input id="phone" name="phone" inputmode="numeric"
  pattern="[0-9]{10,11}"
  title="Enter 10 or 11 digits">

The value must match the pattern when it is not empty. The title can provide a hint for the browser’s validation message, but a visible instruction is better for all users.

Combine constraints

Use required when an empty value is not allowed. Use an appropriate type, such as email, for standard formats. These constraints complement each other; one does not replace the others.

Common mistakes