I am trying to validate an input on an angular 2 reactive form so that only numbers with two decimal places are valid.
I am using the Validators.pattern('^\d+\.\d{2}$')
method to match against a regex pattern. According to https://regex101.com/r/1DbMZq/1 my regex matches correctly but when I use it in my form it is always invalid.
Here is a plunker illustrating the problem: https://plnkr.co/edit/TpBZtgNNww4CnTwQFtef?p=preview
Any ideas what I'm doing wrong?
I assume you have to pass Regexp
type instead of string
like:
Validators.pattern(/^\d+\.\d{2}$/)
Here's updated plunker
If you use string as parameter of the pattern
method then your Regexp will be changed as shown below
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With