Rails 3 validate IPv4 and IPv6 format

倖福魔咒の 提交于 2019-12-07 03:26:47

问题


I know the validation format for IPv4 and IPv6. But not sure how I can combine them so atleast one format should be true. Here is my validation

validates :src_ip_addr, :presence => true, :uniqueness => true,
            :format => { :with => Resolv::IPv4::Regex, :message => "Not an valid IPv4 format"}

  validates :src_ip_addr, :presence => true, :uniqueness => true,
            :format => { :with => Resolv::IPv6::Regex, :message => "Not an valid IPv6 format"}

How I can combine them so if one format is correct then validation should work. Should fail only if ipv4 and ipv6 format is not correct.

Thanks.


回答1:


You can also combine them with Regexp.union:

:format => { :with => Regexp.union(Resolv::IPv4::Regex, Resolv::IPv6::Regex) ...


来源:https://stackoverflow.com/questions/16965697/rails-3-validate-ipv4-and-ipv6-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!