Forum: Ruby on Rails time textfield validation (hh:mm)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2009-06-02 15:00
(Received via mailing list)
I am trying to find the best way in validating a textfield
'start_time' in a form,
 based on the expected strftime format ("%I:%M%p")

if  a user  don't enter the correct format like " 01:45PM"    hh:mm in
12-hours (with PM/AM ) then should raise an error
I know it's possible to validate using gsub , but I wonder if there is
any possibility to raise an ArgumentError in parsing the value in a
Date/Time  function...

I tried

start_time = "89"
t = Time.parse(Time.now.to_date.to_s + "," + start_time)
but it doesn't raise an error...

erwin
4ab2692ba129cb296d4f2e90ba5241da?d=identicon&s=25 Sandip Ransing (san2821)
on 2009-06-02 17:05
(Received via mailing list)
Hi

You need not bother about date/time validations.
There is plugin called validates_date. just download & install it.
in your model,  add following line and you are done.

validates_date :field_name

Cheers !
Sandip

On Tue, Jun 2, 2009 at 8:59 AM, Erwin <yves_dufour@mac.com> wrote:

>
>
--
Ruby on Rails Developer
http://sandip.sosblog.com
www.joshsoftware.com
This topic is locked and can not be replied to.