ngx-custom-validators icon indicating copy to clipboard operation
ngx-custom-validators copied to clipboard

Feature Request: digits with decimals

Open pasevin opened this issue 6 years ago • 2 comments

Nice library! I was looking forward to using it, but didn't find the only validator I need at the moment :)

I would like to validate digits with certain number of decimal places.

If decimal places set to 2: 0.12 -> Valid 0.123 -> Invalid

Maybe this can be helpful (my 2 decimal places validator):

export class DecimalValidator {
	static validate(formGroup: FormGroup): any {
		if (formGroup.pristine || !formGroup.value) {
			return undefined;
		}

		const TWO_DECIMAL_REGEXP = /^\d*(?:[.,]\d{1,2})?$/;
		formGroup.markAsTouched();
		if (TWO_DECIMAL_REGEXP.test(formGroup.value)) {
			return undefined;
		}
		return {
			invalidDecimal: true
		};
	}
}

pasevin avatar Jul 17 '19 19:07 pasevin

Thanks, I will check it for the next release.

rsaenen avatar Aug 07 '19 06:08 rsaenen

You can use a pattern validator for this like the one in your code.

beradrian avatar Oct 21 '20 08:10 beradrian