Jonathan Clark
Jonathan Clark

Reputation: 20538

Validate format of time

I am developing an Rails 4.1.4 web app and I need to validate that the users input is correct. The input is time in this format 12:00 (HH:MM). I just need to check that there are first 2 integers before the : and two integers after. How can I do this in a validation? I rather not use a gem for this simple task. I think it could be done with a regex?

Thanks!

Upvotes: 0

Views: 494

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

You want validates_format_of:

validates_format_of :time, with: /\A\d{2}:\d{2}\z/

Upvotes: 2

Related Questions