0

I am using rails application

I want to allow price validation where min value : 0.01 and max value : 24

Also number start with allow:

  • Examples of formats allowed .23, .2, 1.23, 0.25, 5, 6.3
  • Minimum value of 0.01
  • Maximum value of 24

What is model validation for this?

Robin
  • 9,415
  • 3
  • 34
  • 45
harsh4u
  • 2,550
  • 4
  • 24
  • 39

2 Answers2

2

You can use the following:

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

Regular expression visualization

Demo

Ulugbek Umirov
  • 12,719
  • 3
  • 23
  • 31
1

I would first parse the strings for the any and all floats,

[-+]?(\d*[.])?\d+

then run comparison operators (x<=24 && x>=0.001) on the results to filter the permitted values.

There is no sense trying to do it all in one step with regex unless you are really strapped for resources.

remdezx
  • 2,939
  • 28
  • 49
FutureTechLab
  • 142
  • 1
  • 10