Skip to main content

length

Validate that a string has exactly N characters.

Signature

NguardValidators.String.length(n: number): ValidatorFn
ParameterTypeDescription
nnumberThe required string length

Reactive forms

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

const code = new FormControl('', [NguardValidators.String.length(6)]);

Template-driven forms

<input ngModel name="code" [nguardLength]="6" />

Error key

{ length: true }

Notes

  • Length is value.length — for strings with surrogate pairs (some emoji), this counts code units, not code points.
  • length(0) accepts empty strings.
  • null, undefined, non-string inputs all fail.

See also