我的领域是:
我需要验证正值的tax rate
和tax amount
。
我写了这个验证:
:format => { :with => /\A[+]?\d+\Z/}
但它没有采用像4.67
这样的小数点数字。
它给我一个错误。
什么类型的验证将适用于整数和浮点值?
例如:2
,57
,54.56
应该通过,但-2.56
,-87
应该失败。
答案 0 :(得分:121)
这不起作用吗?
validates :your_field, :numericality => { :greater_than_or_equal_to => 0 }
(猜测遵守规则的税收会更正确:)
validates :your_field, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 100 }
答案 1 :(得分:8)
您可以使用:
validates :tax_rate, inclusion: { in: 0..5 }
它允许使用以下值: 0,2,1.2,3.2
希望它有所帮助!
答案 2 :(得分:0)
您可以使用validates_numericality_of :amount, :greater_than => 0.0