How can I properly validate a subdomain format?
Here's what I've got:
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
Question:
Is there a standard REGEX subdomain validation like there is for email? What is the best REGEX for subdomain to use?
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }, allow_blank: true