# Birthdate validation

**URL:** <https://www.ruby-forum.com/t/birthdate-validation/119143>\
**Category:** Rails\
**Created:** [November 3, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/birthdate-validation/119143 "2007-11-03T18:59:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thiago\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@Thiago\_G](https://www.ruby-forum.com/u/Thiago_G)\
**Post date:** [November 3, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/birthdate-validation/119143/1 "2007-11-03T18:59:37Z")

</div>

Hello everyone,

I’m almost cracking my head trying to do this birthdate validation. It  
turns out that I can only accept users with at least 18 years old and  
I’m trying to validate it writing this code on my user.rb fil at app/  
models

class User \< ActiveRecord::Base  
validates\_presence\_of :full\_name  
validates\_presence\_of :street\_address  
validates\_presence\_of :city  
validates\_presence\_of :state  
validates\_presence\_of :country  
validates\_presence\_of :zip\_code  
validates\_numericality\_of :zip\_code, :message =\> “must contain only  
numbers.”  
validates\_presence\_of :phone  
validates\_numericality\_of :phone, :message =\> “must contain only  
numbers.”  
validates\_presence\_of :email  
validates\_format\_of :email, :with =\> /^([^@\s]+)@((?:[-a-z0-9]+.)+[a-  
z]{2,})$/i  
validates\_acceptance\_of :policy\_agreement, :message =\> “must be  
accepted. You have to read and agree with the Aykall Policies before  
signing in.”  
validates\_multiparameter\_assignments :message =\> " is not a valid  
date."  
age\_limit = Date.new(Date.today.year, Date.today.month,  
Date.today.day).change(:year =\> Date.today.year - 18)  
validates\_numericality\_of :birth\_date, :less\_than =\>  
age\_limit, :message =\> “is invalid. You have to be at least 18 years  
old to sign in.”  
end

I’m getting the error message “undefined method `change’ for #\<Date:  
4908815/2,0,2299161\>” but, honestly, I don’t know what else to try.

Can anyone help me?

Thanks,  
Thiago G.

---

<div class="post-metadata">

**Author:** ![Thiago\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@Thiago\_G](https://www.ruby-forum.com/u/Thiago_G)\
**Post date:** [November 3, 2007, 7:13pm UTC](https://www.ruby-forum.com/t/birthdate-validation/119143/2 "2007-11-03T19:13:04Z")

</div>

On Nov 3, 2007, at 10:59 AM, Thiago G. wrote:

> end  
> Have a look at this info here:

[http://snippets.dzone.com/posts/show/3048](http://snippets.dzone.com/posts/show/3048)

– gw

---

<div class="post-metadata">

**Author:** ![Thiago\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@Thiago\_G](https://www.ruby-forum.com/u/Thiago_G)\
**Post date:** [November 3, 2007, 7:13pm UTC](https://www.ruby-forum.com/t/birthdate-validation/119143/3 "2007-11-03T19:13:04Z")

</div>

On 3 Nov 2007, at 17:59, Thiago G. wrote:

> validates\_numericality\_of :birth\_date, :less\_than =\>  
> age\_limit, :message =\> “is invalid. You have to be at least 18 years  
> old to sign in.”  
> end

change only exists on time, not date. Regardless this won’t work  
properly: the above is only evaluated when the code is first loaded,  
so say you start up your app on the first of december 2007 it would  
check that they’re born before 1st december 1989. A month later it  
will check against that date of first of december.  
I think you’d be far better off with a custom validation method.

Fred

---

<div class="post-metadata">

**Author:** ![Thiago\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19b73/32.png) [@Thiago\_G](https://www.ruby-forum.com/u/Thiago_G)\
**Post date:** [November 4, 2007, 12:07am UTC](https://www.ruby-forum.com/t/birthdate-validation/119143/4 "2007-11-04T00:07:23Z")

</div>

Thanks man.

It worked with:

validates\_multiparameter\_assignments :message =\> " is not a valid  
date."  
validates\_each :birth\_date do |record, attr, value|  
record.errors.add attr, “is not a valid date. You must be at  
least 18 years old to sign in.” if value \> Date.new((Date.today.year -  
18),(Date.today.month),(Date.today.day))  
end
