amg
amg

Reputation: 71

Validate last letters of input field in Rails

How would I validate the last letters of a link which is posted into my model?

Example:

[dropbox.com/dajnkqjknw7/file.mp3]
[dropbox.com/dajnkqjknw7/file.ogg]
[dropbox.com/dajnkqjknw7/file.wav]

I just want to check/validate if the last 3 (or 4) letters end on .mp3, .ogg or .wav, how would I use that within my model, use a Regex in combination with validates?

Current beat.rb model:

class Beat < ApplicationRecord
  validates :bpm, format: { with: /\A\d+\z/ }, length: { maximum: 3 }
  validates :link, presence: true REGEX IN HERE?
end

Upvotes: 0

Views: 36

Answers (1)

xploshioOn
xploshioOn

Reputation: 4115

You can use something like this

validates :link, presence: true, format: { with: /.(mp3|ogv|wav)\z/}

where you have a dot . and after one of the valid extensions (mp3|ogv|wav), just at the end of the line \z

Upvotes: 1

Related Questions