Skip to main content

digits

Validate that an integer has exactly N digits (sign and decimal point excluded).

Signature

NguardValidators.Number.digits(n: number): ValidatorFn
ParameterTypeDescription
nnumberThe required digit count

Reactive forms

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

const pin = new FormControl('', [NguardValidators.Number.digits(4)]);

Template-driven forms

<input ngModel name="pin" [nguardDigits]="4" />

Error key

{ digits: true }

Notes

  • The value must be an integer — floats fail (1.23 is invalid for any digits(n)).
  • Sign is excluded from the count: -1234 has 4 digits.
  • Numeric strings work — '1234' passes digits(4).
  • null, undefined, empty string, NaN, Infinity, non-numeric strings all fail.

See also