Rails 3:验证IP字符串 [英] Rails 3: Validate IP String
本文介绍了Rails 3:验证IP字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Rails 3中,是否有内置方法来查看字符串是否为有效的IP地址?
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?
推荐答案
在Rails 3中使用ActiveRecord进行验证的Rails方法是:
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 }
此处的资源很好: 查看全文