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?
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 :
"^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$"
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:
/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.
([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/.match("#{@systemIP}")
The Rails way to validate with ActiveRecord in Rails 3 is:
@ip_regex = /^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$/
validates :gateway,
:presence => true,
:uniqueness => true,
:format => { :with => @ip_regex }
Good resource here: http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/