Rails 3: Validate IP String

Dex picture Dex · Sep 21, 2010 · Viewed 11.6k times · Source

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?

Answer

Jack picture Jack · Sep 26, 2012

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 }