Validates_format_of

Hi,

I have this in one of my models:

validates_format_of :amount, :with => /\A[0-9]{1,5}.[0-9]{2}\Z/

If I enter an amount that ends with “.00”, eg. 123.00 or 300.00 it
fails, and I cannot understand why. It works with 123.11 or 300.01,
etc.

I have also tried:

validates_format_of :amount, :with => /\A[0-9]{1,5}.[0-9][0-9]\Z/

but it gives the same result (ie. doesn’t work as I would have thought
it would).

Am I missing something?

I’m using JRuby on Netbeans under Windows Vista if that is relevant.

Andrew.

Try telling ruby that a variable contains the value 300.00, it will
say:

a = 300.00
=> 300.0
a
=> 300.0

And there’s your answer. You will have to tell Ruby that this is a so-
called “double”. to_d didn’t work in my local environment, but that
Ruby version is quite old. Try to to find how to convert your object
into a double.

Hope this helps!

Kind regards,
Jaap H.
w. http://www.relywebsolutions.nl

On 8 aug, 14:28, Andrew M. [email protected]

jhaagmans wrote:

You will have to tell Ruby that this is a so-
called “double”.

I don’t think that will help at all. However, I believe there’s a
method called validates_numericality_of that may be of use…

Also, [0-9] is never necessary. Just use \d .

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Thanks all for your suggestions. It’s definitely dropping the final “0”
as Jaap suggested.

I think I can work around it by changing the regular expression to:

/\A[0-9]{1,5}.[0-9]{1,2}\Z/

Also just as a side note I’m new to Ruby / Rails having previously
worked with ColdFusion and Java, and I have to say this is a really
helpful community on this list. Great stuff.

Right, as I said, I’ve never done something like it, but at least we
know what’s wrong.

On 8 aug, 16:05, Marnen Laibow-Koser <rails-mailing-l…@andreas-

Andrew M. wrote:

Thanks all for your suggestions. It’s definitely dropping the final “0”
as Jaap suggested.

I think I can work around it by changing the regular expression to:

/\A[0-9]{1,5}.[0-9]{1,2}\Z/

Apparently you did not read my earlier post in this thread. Please do
so before you go further.

Also just as a side note I’m new to Ruby / Rails having previously
worked with ColdFusion and Java, and I have to say this is a really
helpful community on this list. Great stuff.

I’m a former CF developer myself. If there’s any way that I can provide
Rails insight on something CF-related, please feel free to ask.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Conrad T. wrote:

On Sat, Aug 8, 2009 at 5:28 AM, Andrew M. <
[email protected]> wrote:

Andrew.

Andrew, does the following work for you?

/^[0-9]*.[0-9]{2}$/

I’m not Andrew, but I can tell you that that will have the exact same
problem as his original regex.

Good luck,

-Conrad

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On Sun, Aug 9, 2009 at 9:08 AM, Marnen Laibow-Koser <
[email protected]> wrote:

Andrew, does the following work for you?

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I was able to successfully match the following cases:

300.00
123.00
123.11
300.01

-Conrad

Conrad T. wrote:

On Sun, Aug 9, 2009 at 9:08 AM, Marnen Laibow-Koser <
[email protected]> wrote:

Andrew, does the following work for you?

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

I was able to successfully match the following cases:

300.00
123.00
123.11
300.01

That’s true. But if you actually take the time to read the entire
thread, you will find that the issue is elsewhere. Please actually read
the whole thread before posting.

-Conrad

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

On Sat, Aug 8, 2009 at 5:28 AM, Andrew M. <
[email protected]> wrote:

Andrew.

Andrew, does the following work for you?

/^[0-9]*.[0-9]{2}$/

Good luck,

-Conrad

On Sat, Aug 8, 2009 at 7:05 AM, Marnen Laibow-Koser <
[email protected]> wrote:

jhaagmans wrote:

You will have to tell Ruby that this is a so-
called “double”.

I don’t think that will help at all. However, I believe there’s a
method called validates_numericality_of that may be of use…

validates_numericality_of only determines whether something is numeric

Also, [0-9] is never necessary. Just use \d .

[0-9] is just another way of writing \d. Thus, it’s really a personal
preface and
both are clearly documented in the PixAxe.

Thanks my friend whit this my :price field is correct!

validates_format_of :precio, :with => /\A[0-9]{1,5}.[0-9]{1,2}\Z/,
:message => “”

I love Rails!!

Conrad T. wrote:
[…]

validates_numericality_of only determines whether something is numeric

That’s true. And that may be all that the OP really needs here, which
is why I suggested it.
[…]

[0-9] is just another way of writing \d. Thus, it’s really a personal
preface and
both are clearly documented in the PixAxe.

I’m aware of that. But \d is 2 characters instead of 5, and I believe
that is the better choice from a readability standpoint.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]