Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 reactive form validate pattern for number with two decimal places [duplicate]

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?

like image 206
Amanda Kitson Avatar asked Nov 30 '16 18:11

Amanda Kitson


1 Answers

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

like image 106
yurzui Avatar answered Nov 09 '22 23:11

yurzui