Angular 2 active form validation pattern for two decimal places

I am trying to test angular 2 reactive input, so only numbers with two decimal places are valid.

I am using the Validators.pattern('^\d+\.\d{2}$') method to match the regex pattern. According to https://regex101.com/r/1DbMZq/1 my regular expression matches correctly, but when I use it in my form, it is always incorrect.

Here is a plunker illustrating the problem: https://plnkr.co/edit/TpBZtgNNww4CnTwQFtef?p=preview

Any ideas what I'm doing wrong?

+7
regex validation angular angular2-forms
source share
1 answer

I assume that you need to pass a Regexp type instead of a string , for example:

 Validators.pattern(/^\d+\.\d{2}$/) 

Here plunker is updated

If you use the string parameter as the parameter to the pattern method, then your Regexp will be modified as shown below.

enter image description here

+9
source share

All Articles