# Time textfield validation (hh:mm)

**URL:** <https://www.ruby-forum.com/t/time-textfield-validation-hh-mm/169336>\
**Category:** Rails\
**Created:** [June 2, 2009, 3:00pm UTC](https://www.ruby-forum.com/t/time-textfield-validation-hh-mm/169336 "2009-06-02T15:00:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [June 2, 2009, 3:00pm UTC](https://www.ruby-forum.com/t/time-textfield-validation-hh-mm/169336/1 "2009-06-02T15:00:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [June 2, 2009, 5:05pm UTC](https://www.ruby-forum.com/t/time-textfield-validation-hh-mm/169336/2 "2009-06-02T17:05:51Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
Ruby on Rails Developer

> **[Blogs - Neuigkeiten - Artikel - Webseiten - Medien](https://www.sosblog.com/)**
>
> Auf SOSblog geht es um aktuelle Blogs, Neuigkeiten, Artikel, Webseiten und Medien. Wir bringen interessante neue Webseiten aus den Tiefen des Webs.

> **[Josh Software | Custom Software Development Services & Solutions](https://joshsoftware.com/)**
>
> JoshSoftware offers custom software development Services & Solution that provides enterprise, software development services for web & mobile.
