ngx-custom-validators
ngx-custom-validators copied to clipboard
Feature Request: digits with decimals
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
};
}
}
Thanks, I will check it for the next release.
You can use a pattern validator for this like the one in your code.