I want to put restriction on a field so that user cannot enter white/blank space. I have tried multiple solutions but nothing works.
Following are some solutions that I have tried -
validates :promo_code, format: { with: /\A[a-zA-Z0-9]+\Z/ }
normalize_attribute :promo_code, :with => :strip
validate :check_empty_space
def check_empty_space
if self.promo_code.match(/\s+/)
errors.add(:attribute, "No empty spaces please :(")
end
end
validates :promo_code, format: { without: /\s/, message: "must contain no spaces" }
Please suggest something that actually works.