digits
Validate that an integer has exactly N digits (sign and decimal point excluded).
Signature
NguardValidators.Number.digits(n: number): ValidatorFn
| Parameter | Type | Description |
|---|---|---|
n | number | The 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.23is invalid for anydigits(n)). - Sign is excluded from the count:
-1234has 4 digits. - Numeric strings work —
'1234'passesdigits(4). null,undefined, empty string,NaN,Infinity, non-numeric strings all fail.
See also
digitsBetween— range formminDigits/maxDigits— single-bound variantsinteger— integer check without digit-count constraint