ruby - Validating minutes and seconds in Rails -
i'm trying validate time based attribute called duration
1 of models. attribute, accept along lines of 01:30
valid value. goal have 4 digit time-code (minutes , seconds) colon in between two. both minutes , seconds limit in range 59
, cannot have 00:00
value. regex have doesn't seem 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)
, rest of regex 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
Comments
Post a Comment