Question: What is the best way to validate a cost/price input by a user, validation rules below:

* Examples of formats allowed .23, .2, 1.23, 0.25, 5, 6.3 (maximum of two digits after decimal point)
* Minimum value of 0.01
* Maximum value of 9.99

Answer: 
### Check the price and verify the format

```
#rails 3    
validates :price, :format => { :with => /\A\d+(?:\.\d{0,2})?\z/ }, :numericality => {:greater_than => 0, :less_than => 10}

#rails 2
validates_numericality_of :price, :greater_than => 0, :less_than => 10    
validates_format_of :price, :with => /\A\d+(?:\.\d{0,2})?\z/

```
For client side validations you can use a jQuery plugin like [this one](http://docs.jquery.com/Plugins/validation) that allows you to define different valid formats for a given input.

For server side validations and according to this [question/answer](https://stackoverflow.com/questions/1019939/ruby-on-rails-best-method-of-handling-currency-money) maybe you should use a `decimal` column for `price` in which you can define values for `precision` and `scale`, `scale` solves the two digits after decimal point restriction.

Then to validate the numericality, minimum and maximum value you can use the next validation method:

```
validates_numericality_of :price, :greater_than => 0, :less_than => 10

```