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?
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?
You can also just call standard's library
IPAddr.new
that will parse subnets, IPV6 and other cool things: (IPAddr) and returnnil
if the format was wrong.Just do:
Just wanted to add that instead of writing your own pattern you can use the build in one
Resolv::IPv4::Regex
You can use
Resolv::IPv4::Regex
as Jack mentioned below if you don't need to accept subnets.If you need to accept it, activemodel-ipaddr_validator gem may help you. (disclaimer: I'm the author of the gem)
You can also use Regexy::Web::IPv4 which can match ip addresses with port numbers too.
i dont know about RoR a lot, but if you dont find any built in method for validation of IP Address.
Try on this regular expression :
to validate the IP Address.
I recently used it in one module so had it on desktop.
You should use a
Regular Expression
Here is one that does what you want: