fre*_*est 34 validation ruby-on-rails
验证用户的成本/价格输入的最佳方法是什么,验证规则如下:
rwi*_*ams 69
#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/
Run Code Online (Sandbox Code Playgroud)