Formify Docs

Pattern

Validate a value against a custom regular expression

Pattern

The pattern rule validates a value against a custom regular expression. It is the escape hatch for any format not covered by a built-in validator.

Configuration

Config flagTypeDescription
patternRegExpThe expression the value must match.
pattern[RegExp, string]Tuple form — the expression plus a custom message.
validationSchema: {
  license: { pattern: /^[A-Z]{2}-\d{5}$/ },   // e.g. AB-12345
}

With a custom message:

license: { pattern: [/^[A-Z]{2}-\d{5}$/, "License must look like AB-12345"] }

V builder

import { V } from "formify-js";

V.pattern(/^[A-Z]{2}-\d{5}$/)
V.pattern(/^[A-Z]{2}-\d{5}$/, "License must look like AB-12345")

Default message

Format is invalid

Things to know

  • The expression is tested against the value with .test(). Use ^ and $ anchors when you want the entire value to match — otherwise a partial match passes.
  • Empty values pass unless required: true is also set.
  • pattern accepts any RegExp, so it covers non-string values too (e.g. matching against String(value) when needed).

On this page