# Problems with validates\_format\_of

**URL:** <https://www.ruby-forum.com/t/problems-with-validates-format-of/150667>\
**Category:** Rails\
**Created:** [October 11, 2008, 2:34am UTC](https://www.ruby-forum.com/t/problems-with-validates-format-of/150667 "2008-10-11T02:34:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [October 11, 2008, 2:34am UTC](https://www.ruby-forum.com/t/problems-with-validates-format-of/150667/1 "2008-10-11T02:34:21Z")

</div>

hi,  
i’m trying to validate the format of a date and have the following  
regular code:

validates\_presence\_of :dob  
validates\_format\_of :dob,  
:with =\> /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/,  
:unless =\> Proc.new { |u| u.dob.blank? }

i’ve checked the regexp independently and it works, and i’ve put this  
regexp with telephone, and it checks the dates correctly (telephone is a  
integer type), however when i do it with dob it’ll give me some strange  
results:  
23-12-2008 will be valid, while 23-13-2008 won’t. so i’m guessing it’s  
checking whether dates are valid completely independently from my  
validations, however this isn’t what i want.

am i missing something here?  
thanks  
daniel

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [October 11, 2008, 2:41am UTC](https://www.ruby-forum.com/t/problems-with-validates-format-of/150667/2 "2008-10-11T02:41:02Z")

</div>

the error message i get if i write 23-13-2008 is “dob can’t be blank”

Daniel F. wrote:

> hi,  
> i’m trying to validate the format of a date and have the following  
> regular code:
> 
> validates\_presence\_of :dob  
> validates\_format\_of :dob,  
> :with =\> /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/,  
> :unless =\> Proc.new { |u| u.dob.blank? }
> 
> i’ve checked the regexp independently and it works, and i’ve put this  
> regexp with telephone, and it checks the dates correctly (telephone is a  
> integer type), however when i do it with dob it’ll give me some strange

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [October 11, 2008, 2:56am UTC](https://www.ruby-forum.com/t/problems-with-validates-format-of/150667/3 "2008-10-11T02:56:06Z")

</div>

On Oct 11, 1:41 am, Daniel F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> the error message i get if i write 23-13-2008 is “dob can’t be blank”

At the point the validation runs the string ‘23-13-2008’ has been  
turned into an actual date (ie an instance of Time or Date depending  
on your column type), so validating its format is nonsensical.

Fred

---

<div class="post-metadata">

**Author:** ![arachnd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/278dde/32.png) [@arachnd](https://www.ruby-forum.com/u/arachnd)\
**Post date:** [October 11, 2008, 3:03am UTC](https://www.ruby-forum.com/t/problems-with-validates-format-of/150667/4 "2008-10-11T03:03:38Z")

</div>

thank you I just noticed this as you wrote it ☹  
shit  
i’ve been trying to fix this for four hours  
damn ruby… ☹  
Frederick C. wrote:

> On Oct 11, 1:41ï¿½am, Daniel F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > the error message i get if i write 23-13-2008 is “dob can’t be blank”
> 
> At the point the validation runs the string ‘23-13-2008’ has been  
> turned into an actual date (ie an instance of Time or Date depending  
> on your column type), so validating its format is nonsensical.
> 
> Fred
