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/

```
You can build custom validations.Lets say, for example the second case:

```
validate :price_has_to_be_greater_than_minimum

def price_has_to_be_greater_than_minimum
  errors.add(:price, "price has to be greater than 0.01") if
  !price.blank? and price > 0.01
end

```

More on this, in the Rails Guides, [here](http://guides.rubyonrails.org/active_record_validations_callbacks.html#custom-methods).