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

27,772

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

Share:
27,772
Amanda Kitson
Author by

Amanda Kitson

Updated on July 09, 2022

Comments

  • Amanda Kitson
    Amanda Kitson almost 2 years

    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?

  • Casimir et Hippolyte
    Casimir et Hippolyte over 7 years
    You should add that '\\d+\\.\\d{2}' is also a possibility.