【发布时间】:2020-02-17 20:27:34
【问题描述】:
如何正确验证子域格式?
这是我得到的:
validates :subdomain, uniqueness: true, case_sensitive: false
validates :subdomain, format: { with: /\A[A-Za-z0-9-]+\z/, message: "not a valid subdomain" }
validates :subdomain, exclusion: { in: %w(support blog billing help api www host admin en ru pl ua us), message: "%{value} is reserved." }
validates :subdomain, length: { maximum: 20 }
before_validation :downcase_subdomain
protected
def downcase_subdomain
self.subdomain.downcase! if attribute_present?("subdomain")
end
问题:
是否有像电子邮件一样的标准 REGEX 子域验证?子域使用的最佳正则表达式是什么?
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }, allow_blank: true
【问题讨论】:
-
域和子域可以是数字、文本或混合字符,但不能是
%,$,^,&等字符... -
为什么不使用
URI.parse()? -
@max 如果实际域以
-结尾无效,那么子域如何有效? namecheap.com/domains/registration/… -
你能给我们一个有效和无效值的例子吗?
-
我重新表述了这个问题以明确我在寻找什么
标签: ruby-on-rails regex ruby subdomain