Currency Validation in Ruby on Rails

This brief post will show you how to validate currency in your Ruby on Rails app.


Published on:October 10, 2013

If you ever need to validate currency in your Ruby on Rails app, simply add the following line of code to the model of your choice.

app/models/yourmodel.rb:

validates :price, :presence => true,
            :numericality => true,
            :format => { :with => /^\d{1,4}(\.\d{0,2})?$/ }
This will allow values of up to $9999.99 with an optional decimal place (if the decimal is present, no more than 2 digits must follow.) Tweak as needed.