Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: digits with decimals #25

Open
pasevin opened this issue Jul 17, 2019 · 2 comments
Open

Feature Request: digits with decimals #25

pasevin opened this issue Jul 17, 2019 · 2 comments

Comments

@pasevin
Copy link

pasevin commented Jul 17, 2019

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
		};
	}
}
@rsaenen
Copy link
Owner

rsaenen commented Aug 7, 2019

Thanks, I will check it for the next release.

@beradrian
Copy link

beradrian commented Oct 21, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants