Skip to main content
Version: 0.1.2

Function: pattern()

Imported from @react-logic/utils

pattern<N>(regex, message?, name?): ValidatorEntry<string> & object

String must match regex. Pairs with the HTML pattern attribute — mirrors regex.source (flags ignored, which matches HTML semantics).

Two pattern() validators on one field need distinct names — pass the third arg to disambiguate:

validators: [
pattern(/[A-Z]/, 'Needs uppercase', 'uppercase'),
pattern(/\d/, 'Needs digit', 'digit'),
]
// errors.password.uppercase, errors.password.digit

Type Parameters

N

N extends string = "pattern"

Parameters

regex

RegExp

message?

string = 'Invalid format'

name?

N = ...

Returns

ValidatorEntry<string> & object