本文介绍了Rails 3验证IPv4和IPv6格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道IPv4和IPv6的验证格式.但不确定如何将它们组合在一起,因此至少一种格式应该是正确的.这是我的验证
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"}
我如何将它们组合在一起,因此,如果一种格式正确,则验证应该起作用.仅当ipv4和ipv6格式不正确时才会失败.
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.
谢谢.
推荐答案
您也可以将它们与Regexp.union
组合:
You can also combine them with Regexp.union
:
:format => { :with => Regexp.union(Resolv::IPv4::Regex, Resolv::IPv6::Regex) ...
这篇关于Rails 3验证IPv4和IPv6格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!