0

in angular 6 reactive forms.

regular expression of : 9999.99 OR 0000.99 ( 2 decimal) and not more then 10000 number, wrote below ( basically 0000.01 to 9999.99 should be acceptable)

Edit : 1 to 9999 also 0000.01 to 9999.99 also )

const currency ="(?!0)\\d+(?:\\.\\d+)?$'";
    RatesFormArray.push(new FormControl('', [Validators.required, Validators.pattern(this.currency)]));

but it is not working. is angular reactive forms pattern validator works same way like regex syntax new RegExp() from javascript?

what i have tried is : https://stackblitz.com/edit/angular-symlaq?file=src%2Fapp%2Fapp.component.ts

2
  • Which samples were not working? Commented Sep 27, 2018 at 4:56
  • Your regex seems wrong. you should correct it first. Commented Sep 27, 2018 at 5:19

2 Answers 2

2

try this regex to find number between 0000.01 to 9999.99

(/^([\d]{0,4})(.|$)([\d]{2,2}|)$/g)

above regex will match only number between 0000.01 to 9999.99, so you should add it i

constcurrency ="\^([\\d]{0,4})(\\.|$)([\\d]{2,2}|)$";
RatesFormArray.push(new FormControl('', [Validators.required,Validators.pattern(this.currency)]));
Sign up to request clarification or add additional context in comments.

3 Comments

just updated my regex, please try again. i have checked same on slackbiz, its working fine there.
what number you are entering there. i see its working . if i enter any number between 0000.01 to 9999.99 then its shows valid otherwise its shows me invalid.
try this - currency ="\^([\\d]{0,4})(\\.|$)([\\d]{2,2}|)$"; check here - stackblitz.com/edit/angular-vh7hat
1

The regex const currency="(?!(^0+(\.0+)?$))^\d{1,4}(\.\d{1,2})?$" will match any number between 0 and 9999 with two decimal points Try here

3 Comments

2 problems - #1 :- it is not accepting 1 to 9999 #2: not sure what need to put in const currency
0 to 0000.00 is allowed in your regex! price cannot be 0 right?
Added negative look ahead to check zero

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.