13

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?

Amanda Kitson
  • 5,477
  • 12
  • 49
  • 73

1 Answers1

12

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

enter image description here

yurzui
  • 205,937
  • 32
  • 433
  • 399