Validate that a value is in a certain range, e.g. 1 <= val <=2

后端 未结 4 1378
逝去的感伤
逝去的感伤 2021-02-12 09:24

I want to validate a number :value to be within 1 or 2

validates :value, :format => { :with => /1|2/, :message => \"Select number..\" }
         


        
4条回答
  •  不要未来只要你来
    2021-02-12 09:47

    if you want it to be a number within 1 and 2 ( 1.5, 1.6839749, etc ) do

    validates_numericality_of :value, :greater_than_or_equal_to => 1, :less_than_or_equal_to => 2, :message => "blah"
    

    may not be what you are looking for but is worth noting,

提交回复
热议问题