0

I have problem with my price validation regular expression.

My pattern does not accept values that I want such as:

  • 101.00
  • 110.00
  • 1000.00
  • 0.50

My pattern accepts values such as:

  • 1.00
  • 1.10
  • 1.11
  • 11.11

Right now, my pattern is ^([1-9]+\.)(\d{2})$.

Desired regexp should not accept 'zero' values like e.g. 0.00 or 0.

Kamil Kiełczewski
  • 53,729
  • 20
  • 259
  • 241
Debugger
  • 21
  • 4

2 Answers2

2

Try

/^(?!0.00)(\d+\.)(\d\d)$/

[
  '0','0.00',
  '101.00','110.00','1000.00','0.50',
  '1.00','1.10','1.11','11.11'
].map(x=>
  console.log( x, /^(?!0.00)(\d+\.)(\d\d)$/.test(x) )
);
Kamil Kiełczewski
  • 53,729
  • 20
  • 259
  • 241
  • thanks! but i found another flaw which is, its accepts values such as below: 00.00, 000.00 – Debugger Nov 07 '19 at 08:10
  • also, is it possible to ignore value IF the price is starting with 0 unless there's a number after decimal points? such as 0101.00 or 01000.00 need to ignore – Debugger Nov 07 '19 at 08:14
1

The expression /^(0\.(?!00)|(?!0)\d+\.)\d\d$/ satisfies all your requirements (so far). It seems easier to handle the cases 0.x and y.z as two alternatives.

Armali
  • 14,228
  • 13
  • 47
  • 141