Home > Net >  Input regular expression pattern typescript reactiveform
Input regular expression pattern typescript reactiveform

Time:01-25

I have some input filed like this

<input type="text" formControl="presentEstimatedValue"
       >

And validation on the field like:

presentEstimatedValue: [null, [[Validators.required,
    Validators.pattern(/^\d{1,8}(?:\.\d )?$/)]]

I wish to allow numbers between 0 and 99999999, with or without a thousands separator, using dot as the separator. That is, the smallest number is 0 and the largest number is either 99999999 or 99.999.999, the latter with the dot thousands separators. I need to support both possible formats.

I have tried with Validation.min and max but this only work with type number :(

CodePudding user response:

You may use the following regex pattern:

^(?:(?=.{1,10}$)\d{1,3}(?:\.\d{3})*|\d{1,8})$

Here is an explanation of the regex pattern:

^                 from the start of the number
(?:
    (?=.{1,10}$)  assert that total length is 0 to 10
    \d{1,3}       match 1 to 3 digits
    (?:\.\d{3})*  then match dot-3 digit groups, zero or more times
    |             OR
    \d{1,8}       match a pure number of 1 to 8 digits
)
$                 end of the number

Here is a demo.

  •  Tags:  
  • Related