Forum: Ruby on Rails validates_numericality_of positive integer

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Eea3feaacbe44706164289d068d94828?d=identicon&s=25 Peter Michaux (Guest)
on 2006-01-19 21:07
(Received via mailing list)

What is the simplest way to validate a positive integer?

validates_numericality_of :foo, :integer_only => true

how do I add the positive part? Do I need another validation statement
for pattern matching or do I have to write a validate() funciton for
my model?

Eea3feaacbe44706164289d068d94828?d=identicon&s=25 Peter Michaux (Guest)
on 2006-01-23 22:52
(Received via mailing list)
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-01-23 23:50
(Received via mailing list)
On 1/23/06, Peter Michaux <> wrote:
> > for pattern matching or do I have to write a validate() funciton for
> > my model?
> >

I'd go with this, personally:
def validate
  unless foo_before_type_cast.to_s =~ /^[+]?\d+$/
    errors.add("foo", "is not a valid number")

500 and +500 are valid, -500, -500.00, 400.12, etc, are not.
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-01-23 23:56
(Received via mailing list)
Peter Michaux wrote:
>> for pattern matching or do I have to write a validate() funciton for
>> my model?
Write your own function, I reckon.
Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-01-24 03:34
(Received via mailing list)
I hacked together a plugin to add a few parameters to
validates_numericality_of based on your needs and my laziness for

>From the README:

Adds the following parameters:
(plus the messages generated as an error)

:gt => ##     "must be greater than ##"
:gte => ##    "must be greater than or equal to ##"
:eq => ##     "must be equal to ##"
:lt => ##     "must be less than ##"
:lte => ##    "must be less than or equal to ##"
:odd => ##    "must be an odd number"
:even => ##   "must be an even number"
:within => ##..## "must be within ## and ##"

Usage: (in your case)

validates_numericality_of :foo, :integer_only => true, :gt => 0

validates_numericality_of :age, :integer_only => true, :within =>

If RoR team would like to integrate into Rails, have at it. I don't do
with rejection, so I won't offer it as a patch unless requested.

Download it at:

I'll get it downloadable via script/plugin in due time. Gotta write a
unit tests first.


Bob Silva
Eea3feaacbe44706164289d068d94828?d=identicon&s=25 Peter Michaux (Guest)
on 2006-01-26 03:28
(Received via mailing list)
Hi Bob,

Thanks for the plugin. I think this should be included in rails as
these are some very standard things to check.

Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-01-26 03:40
(Received via mailing list)
After speaking with a buddy of mine also doing Rails, I need to make
modifications. It shouldn't have the :within since this can already be
accomplished with validates_inclusion_of. As far as including in Rails,
depends what their definition of numericality is. With the current
name, it's not appropriate to have these extra checks in them and they
really aren't critical, just saves some code to validate your input.

But my second plugin, once I polish it off should definitely be
but you'll have to wait for that one :)

Bob Silva
This topic is locked and can not be replied to.