Forum: Ruby on Rails how do I validate a numeric field?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
33478bbfbd0e2bf2737eca60a555af76?d=identicon&s=25 farocco (Guest)
on 2005-12-08 22:23
(Received via mailing list)
Hello,

I have a quanity field in my form that should have a length of no more
than 3.

I tried validate_length_of :qty, :maximum => 3

and this fails

Thanks
Frank
Eb234d1ee9f1dcd334657d7c5c1b1c4d?d=identicon&s=25 Jamie Macey (jamie)
on 2005-12-08 22:31
(Received via mailing list)
Frank,

try this:

validate_format_of :qty, /[1-9][\d]{0,2}/

That should get you a non-zero digit, followed by up to two more digits.

- Jamie
33478bbfbd0e2bf2737eca60a555af76?d=identicon&s=25 farocco (Guest)
on 2005-12-08 22:39
(Received via mailing list)
I get Application error (Rails)

Frank
----- Original Message -----
From: "Jamie Macey" <jamie@bravenet.com>
To: <rails@lists.rubyonrails.org>
Sent: Thursday, December 08, 2005 4:27 PM
Subject: Re: [Rails] how do I validate a numeric field?
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 brianvh (Guest)
on 2005-12-08 22:39
(Received via mailing list)
Frank wrote:
> I have a quanity field in my form that should have a length of no more than 3.
>
> I tried validate_length_of :qty, :maximum => 3
>
> and this fails

Numbers don't have a length. Just because a numeric value is 1001
doesn't mean the variable has a "length" of 4.

What you probably want to use is:

validates_inclusion_of :qty, :in => 0..999

-Brian
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 Bruce Balmer (brucebalmer)
on 2005-12-08 22:43
(Received via mailing list)
I'm guessing, but is "length" meant for strings and it should be
something like validate_with_precision? something like that.

bruce
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 brianvh (Guest)
on 2005-12-08 22:43
(Received via mailing list)
Jamie Macey wrote:
> try this:
>
> validate_format_of :qty, /[1-9][\d]{0,2}/

Just so we're clear, the method is correctly named,
"validates_format_of", not "validate_format_of". At least, that's what
my PDF copy of AWDwR says. But I wouldn't use validates_format_of to
check for a number within a certain range of values. That's what
validates_inclusion_of is for...

-Brian
Eb234d1ee9f1dcd334657d7c5c1b1c4d?d=identicon&s=25 Jamie Macey (jamie)
on 2005-12-08 22:43
(Received via mailing list)
Sorry, validates_format_of (with an S).  I like Brian's answer better
though.
33478bbfbd0e2bf2737eca60a555af76?d=identicon&s=25 farocco (Guest)
on 2005-12-08 22:47
(Received via mailing list)
Thanks,

This worked:

validates_inclusion_of :QTY1, :in => 0..999, :message => "must be
between 0
and 999"

Frank

----- Original Message -----
From: "Brian V. Hughes" <brianvh@alum.dartmouth.org>
To: <rails@lists.rubyonrails.org>
Sent: Thursday, December 08, 2005 4:37 PM
Subject: Re: [Rails] how do I validate a numeric field?
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2005-12-08 23:53
(Received via mailing list)
Hi Frank,

You could use the *validates_numericality_of(*args) *method.

eg
validates_numericality_of :qty, :only_integer => true
http://api.rubyonrails.com/classes/ActiveRecord/Va...

Your model class can then include a validate method.  This method is
called
before the model is saved, and you should protect it.

ie
protected
def validate
  errors.add( :qty, "Should be between 0 and 1000") unless ( :qty.nil?
||
(:qty >= 0 and :qty < 1000))
end

This information came from the Agile web Development with Rails book
modified for this situation.  It is a great book when your starting with
Rails.

Cheers
Dan
This topic is locked and can not be replied to.