MMore
MMore

Reputation: 385

Rails: How to validate for multiple exact lengths of a string?

I want to check a string for exactly 8 or 10 characters.

Is there a possibility with validates_length_of?

Upvotes: 1

Views: 913

Answers (1)

Chris Ledet
Chris Ledet

Reputation: 11628

Just do this

class Foo

  validate :check_length

  def check_length
    unless column.size == 8 or column.size == 10
      errors.add(:column, "length must be 8 or 10") 
    end
  end

end

You get the idea.

Upvotes: 3

Related Questions