我想问是否有办法同时验证字符串作为网址或电子邮件?
例如:
validates_format_of :string, :with => [url_regex, email_regex]
string = 'http://domain.com' => valid
string = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1a7f777b73765a7e75777b737434797577" rel="noreferrer noopener nofollow">[email protected]</a>' => valid
提前致谢!
最佳答案
根据Rails API你不能在数组或其他东西中传递两个正则表达式。解决方案是创建一个匹配 URL 和电子邮件地址的正则表达式,或者按照 Naveed 的建议创建自定义验证。话虽如此,我认为您无法使用正则表达式验证 URL。
关于ruby-on-rails - 验证 Ruby on Rails 中的 url 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9306890/