Expression expressions for comma numbers and optional .00

I want to create a regular expression pattern for entering a number. When the user enters numbers, the comma automatically enters a number, for example, 1.424 or 23.232. This works fine with regex pattern

/^[0-9.,]*$/

But the problem is that this pattern allowed a dot (.) Between numbers. I want to make a regex expression, as the input may allow .00 to go to the end of numbers not between numbers. But .00 is also optional. The allowed number formats are below:

123312131256457.00
1233121312564
9,223,372

Invalid number formats below:

34.343455.3434
34353... 

I spend the lost time on the same, but do not get any solution. Please share your ideas. Thanks in advance.

+4
source share
3

- /^[0-9,]*(\.00)?$/. I.e., .00, . literal .


, - : ^(0|[1-9]\d{0,2}(,?\d{3})*)(\.00)?$

:

0 // OK
01 // Not OK, must start with 1-9 if not 0 or 0.00
1,1,1.00 // Not OK, groups must be 3 digits, if used
0,000.00 // Not OK, should be 0.00
1,000 // OK
123312131256457.00 // OK, groups are optional
1233121312564 // OK, decimals (.00) are optional
9,223,372 // OK
0

:

/^[0-9]{1,3}((,[0-9]{3})*|([0-9])*)(\.[0]{2})?$/

:

^              from the start
[0-9]{1,3}     match 1 to 3 numbers
(,[0-9]{3})*   followed by a comma and three numbers, any number of times
([0-9])*       OR just followed any amount of numbers, with NO commas
(\.[0-9]{2})?  followed by an optional decimal point and two zeroes
$              end

:

Regex101

+3

just something like

/^[0-9,]*(?:\.0+)?$/

but [0-9,]*also allows "3 ,,"

0
source

Source: https://habr.com/ru/post/1661914/


All Articles