0

Currently building a backend administrators control panel for an iOS app I've developed. From this control panel an admin can add items to a store. One of the properties/attributes of a item would be it's price.

I'd like the price in this format only:

Min price: £0.00 and Max price: £000.00

I figured I could create two select dropdown fields then just combine the values before submitted my form.

However I'm wondering if there is a regex I can create that I could include in my form validation. My current price field is a standard text field and right now an admin can enter things like 000.444 4304.34 32.345 and get away with it. The minimum price for items in the store would be 0.00 and the maximum would be 300.00.

Is it possible to create a regex that allow numbers to be entered in this format only? Stopping things like 34.3455, 43.454, 34,54, 3,444, 434334..34 being submitted to the database?

I could do some validation with javascript in the cloud (I use parse.com as a backend database for the ios app and rails is linked up with it) to do some validation but I'd prefer to do client side and server side validation. Don't want the form being submitted unless it's 100% correct.

Would appreciated some advice

Thanks for your time

LondonGuy
  • 10,778
  • 11
  • 79
  • 151

2 Answers2

1

For the front end validation, you can use http://jqueryvalidation.org/documentation/

It offer methods to validate numbers and ranges

And for the model validation, this seems to be the solution

#rails 3
validates :price, :format => { :with => /\A\d+(?:\.\d{0,2})?\z/ }, :numericality => {:greater_than => 0, :less_than => 10}
Rafal
  • 2,576
  • 2
  • 18
  • 13
  • Used this to help with my solution: :numericality => {:greater_than => 0, :less_than => 10 so marked you up. – LondonGuy Aug 03 '14 at 16:49
1

You can use this pattern that forbids leading zeros except for prices lower than 1. Only 2 decimal digits are allowed, but if you want to make it more flexible, you can replace [0-9]{2} with [0-9]{1,2}:

\A(?:[1-9]+[0-9]*|0)(?:\.[0-9]{2})?\z

About the price range, the regex pattern is not the tool for that (It's however possible to do it). The best is to check it programmatically. (see @Rafal answer)

Note: writing \A(?:[1-9]+[0-9]*|0)(?:\.[0-9][0-9])?\z may be a little faster with Ruby. (Or \A(?:[1-9]+[0-9]*|0)(?:\.[0-9][0-9]?)?\z if you allow only one decimal)

Online demo

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
  • This still allows me to enter 300.0. Should only be able to enter a minimum of 2 digits after the decimal and before the decimal a minimum of 1 before and maximum of 3. – LondonGuy Aug 03 '14 at 16:38
  • @LondonGuy: The first version of the pattern I wrote, allows only 2 digits after the dot, if It doesn't work, you have probably made a mistake somewhere. About the minimum 1 and maximum of 3, it is not the role of a regex pattern (since it checks the range), you must delegate this task. – Casimir et Hippolyte Aug 03 '14 at 16:44
  • @LondonGuy: I added an online demo. – Casimir et Hippolyte Aug 03 '14 at 16:48