# Regular Expressions

**URL:** <https://www.ruby-forum.com/t/regular-expressions/184051>\
**Category:** Rails\
**Created:** [February 24, 2010, 11:00am UTC](https://www.ruby-forum.com/t/regular-expressions/184051 "2010-02-24T11:00:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [February 24, 2010, 11:00am UTC](https://www.ruby-forum.com/t/regular-expressions/184051/1 "2010-02-24T11:00:52Z")

</div>

> validates\_format\_of :closing\_date, :with =\> /^\d\d-\d\d-\d\d\d\d$/, :message =\> “is not correct format”

Does this seem correct or is there a better way to do this? Basically, I  
have a closing\_date column in my database which is set as a date field.  
On the front end form, I’ve used a text box for the user to enter the  
date. I’ve entered the data exactly as the regular expression above  
dictates but I can’t seem to make it pass that instance method 😕

Please help.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [February 24, 2010, 11:06am UTC](https://www.ruby-forum.com/t/regular-expressions/184051/2 "2010-02-24T11:06:00Z")

</div>

On Feb 24, 10:00 am, Pale H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > validates\_format\_of :closing\_date, :with =\> /^\d\d-\d\d-\d\d\d\d$/, :message =\> “is not correct format”
> 
> Does this seem correct or is there a better way to do this? Basically, I  
> have a closing\_date column in my database which is set as a date field.  
> On the front end form, I’ve used a text box for the user to enter the  
> date. I’ve entered the data exactly as the regular expression above  
> dictates but I can’t seem to make it pass that instance method 😕

by the time rails validates the record it has already turned it into a  
date.

Fred

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [February 24, 2010, 11:18am UTC](https://www.ruby-forum.com/t/regular-expressions/184051/3 "2010-02-24T11:18:54Z")

</div>

On 24 February 2010 10:07, Pale H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Frederick C. wrote:
> 
> > by the time rails validates the record it has already turned it into a  
> > date.

> Thanks for your swift response. What exactly does this mean for my  
> validation, though?

validates\_presence\_of would probably suffice. If the user entered an  
non-parseable value, the closing\_date field will be nil.

try it and see 🙂

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [February 24, 2010, 11:07am UTC](https://www.ruby-forum.com/t/regular-expressions/184051/4 "2010-02-24T11:07:59Z")

</div>

Frederick C. wrote:

> by the time rails validates the record it has already turned it into a  
> date.
> 
> Fred

Thanks for your swift response. What exactly does this mean for my  
validation, though?

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [February 24, 2010, 11:21am UTC](https://www.ruby-forum.com/t/regular-expressions/184051/5 "2010-02-24T11:21:46Z")

</div>

Michael P. wrote:

> On 24 February 2010 10:07, Pale H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Frederick C. wrote:
> > 
> > > by the time rails validates the record it has already turned it into a  
> > > date.
> 
> > Thanks for your swift response. What exactly does this mean for my  
> > validation, though?
> 
> validates\_presence\_of would probably suffice. If the user entered an  
> non-parseable value, the closing\_date field will be nil.
> 
> try it and see 🙂

I just fail to see why my method wouldn’t work, but I will try - thank  
you.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [February 24, 2010, 11:24am UTC](https://www.ruby-forum.com/t/regular-expressions/184051/6 "2010-02-24T11:24:45Z")

</div>

validate :method\_for\_date

def method\_for\_date  
yours code format specifications  
end

try something like this may be help full

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [February 24, 2010, 11:37am UTC](https://www.ruby-forum.com/t/regular-expressions/184051/7 "2010-02-24T11:37:43Z")

</div>

On 24 February 2010 10:21, Pale H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Michael P. wrote:
> 
> > try it and see 🙂
> 
> I just fail to see why my method wouldn’t work, but I will try - thank  
> you.

You’re doing a regex evaluation on a Date (or DateTime) object… so it  
fails.  
Try these in IRB:

> > Date.today =~ /10/

> > Date.today.to\_s =~ /10/

By definition, the format of the Date converted to a string is defined  
by the to\_s method, so it’s a tautology to change it to a string and  
check its format!  
By all means check that it’s in the future or past, or within whatever  
range you want… but it’s a DATE - so do date operations on it, not  
string operations.

If you want to check what the user _typed_ you need to get to the  
controller and do some validation on params[:closing\_date] (or pass  
that value into an attr\_writer in your model, and do model validations  
on that parameter…)

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [February 24, 2010, 11:29am UTC](https://www.ruby-forum.com/t/regular-expressions/184051/8 "2010-02-24T11:29:03Z")

</div>

Pale H. wrote:

> Michael P. wrote:
> 
> > On 24 February 2010 10:07, Pale H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > Frederick C. wrote:
> > > 
> > > > by the time rails validates the record it has already turned it into a  
> > > > date.
> > 
> > > Thanks for your swift response. What exactly does this mean for my  
> > > validation, though?
> > 
> > validates\_presence\_of would probably suffice. If the user entered an  
> > non-parseable value, the closing\_date field will be nil.
> > 
> > try it and see 🙂
> 
> I just fail to see why my method wouldn’t work, but I will try - thank  
> you.

Sorry, I misread this. I do not want to check for the presence of the  
closing date, I just want to check the format of it against this:  
“30-02-2010”.
