Skip to main content

regex

Validate that a string matches a given regular expression.

Signature

NguardValidators.String.regex(pattern: RegExp): ValidatorFn
ParameterTypeDescription
patternRegExpThe pattern the input must match

Reactive forms

import { FormControl } from '@angular/forms';
import { NguardValidators } from 'ng-nguard';

// Three uppercase letters
const code = new FormControl('', [
NguardValidators.String.regex(/^[A-Z]{3}$/),
]);

Template-driven forms

<input ngModel name="code" [nguardRegex]="/^[A-Z]{3}$/" />

Error key

{ regex: true }

Notes

  • Returns null (valid) if the pattern matches.
  • Returns { regex: true } if it does not.
  • null, undefined, non-string inputs all fail.
  • Anchor your patterns (^...$) when you need full-string matching — regex(/abc/) allows 'xabcy'.

See also