Rails 3: Validate IP String

后端 未结 7 865
孤独总比滥情好
孤独总比滥情好 2020-12-13 18:41

In Rails 3, is there a built in method for seeing if a string is a valid IP address?

If not, what is the easiest way to validate?

7条回答
  •  春和景丽
    2020-12-13 18:50

    Just wanted to add that instead of writing your own pattern you can use the build in one Resolv::IPv4::Regex

    require 'resolv'
    
    validates :gateway, :presence => true, :uniqueness => true,
      :format => { :with => Resolv::IPv4::Regex }
    

提交回复
热议问题