Encodera

Thanks for reading!

Donate to the site — we work hard for you.

HTML & CSS reference

pattern

A pattern (regular expression) the field value must match.

Syntax

<input pattern="[0-9]{5}" title="5 digits">

Parameters

ParameterTypeDescription
patternregular expressionA regular expression the whole value must match.

Examples

A five-digit ZIP code — type letters and press the button:

<form>
  <label>ZIP code
    <input name="zip" pattern="[0-9]{5}" title="Five digits, e.g. 10115" required>
  </label>
  <button>Check</button>
</form>

Length without a regular expression:

<form>
  <input name="nick" minlength="3" maxlength="12" placeholder="3–12 characters" required>
  <button>Save</button>
</form>

Notes

The pattern checks the whole value — you do not need ^ and $. The browser adds the title text to the error message, so describe the format there. An empty field is not checked by pattern — add required if the field is mandatory.

See also

  • required — Makes a field required: the form will not submit while it is empty.
  • type — The kind of an `<input>` field: from text and numbers to a date, colour and file.
  • <input> — An input field; its kind is set by the `type` attribute.

Related recipes

In the tutorial

  • Form validation — Built-in field validation in the browser — required, minlength and maxlength, min and max, pattern, styling errors and novalidate.

Thanks for reading!

Donate to the site — we work hard for you.