-8
Pattern regex = Pattern.compile("^\\d{0,8}\\.\\d{0,4}$");

is working, but if I enter value e.g 5000, it should work. Basically valid value should be equal or less than "99999999.9999".

If value is "123456789" it is invalid.

Decimal point is not mandatory.

Please help.

Ankush soni
  • 1,439
  • 1
  • 15
  • 30
rahul
  • 13
  • 3

2 Answers2

4

Since every floating-point number has many different representations (think 100, 100.0, 1e2, etc), I'd suggest parsing the number into a double, and then using a numeric comparison to establish whether it's within the desired range.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

make the decimal part optional

^\d{0,8}(\.\d{0,4})?$

but i think you mean until only 12345678 but if not

^\d{0,9}(\.\d{0,4})?$

but i'm proposing to have atleast 1 number after a decimal so if the user tries to enter 123. it will fail

^\d{0,9}(\.\d{1,4})?$
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • replace that part `(\.\d{0,4})?` with `(\.\d+)?` to match numbers with more digits after the decimal point but are still smaller from the mathematical aspect – jlordo Nov 22 '12 at 14:00