I'm currently trying to validate a time based attribute called duration
for one of my models. The attribute, would accept something along the lines of 01:30
as a valid value. The goal is to have a 4 digit time-code (minutes and seconds) with a colon in between the two. Both minutes and seconds limit in range 59
and cannot have 00:00
as a value. The regex I currently have doesn't seem to work:
validates :duration, presence: true, format: {with: /A([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]z/}
Use negative lookahead(?!00:00)
, the rest of the regex is a simple minutes/seconds validation.
/\A(?!00:00)[0-5][0-9]:[0-5][0-9]\Z/
if subject =~ /\A(?!00:00)[0-5][0-9]:[0-5][0-9]\Z/
# Successful match
else
# Match attempt failed
end
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments