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. -- Posted via ruby-forum.com.
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 Haagmans w. relywebsolutions.nl On 8 aug, 14:28, Andrew Myers <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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. > -- > Posted viahttp://www.ruby-forum.com.
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 marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via ruby-forum.com.
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- s.net> 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... > > Also, [0-9] is never necessary. Just use \d . > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com.
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. -- Posted via ruby-forum.com.
Andrew Myers 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 marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via ruby-forum.com.
On Sat, Aug 8, 2009 at 5:28 AM, Andrew Myers < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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.Andrew, does the following work for you? /^[0-9]*\.[0-9]{2}$/ Good luck, -Conrad> > -- > Posted via ruby-forum.com. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Conrad Taylor wrote:> On Sat, Aug 8, 2009 at 5:28 AM, Andrew Myers < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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, > > -ConradBest, -- Marnen Laibow-Koser marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via ruby-forum.com.
On Sun, Aug 9, 2009 at 9:08 AM, Marnen Laibow-Koser < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Conrad Taylor wrote: > > On Sat, Aug 8, 2009 at 5:28 AM, Andrew Myers < > > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 > marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org >I was able to successfully match the following cases: 300.00 123.00 123.11 300.01 -Conrad> <marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org> > -- > Posted via ruby-forum.com. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Conrad Taylor wrote:> On Sun, Aug 9, 2009 at 9:08 AM, Marnen Laibow-Koser < > rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > >> > Andrew, does the following work for you? >> >> Best, >> -- >> Marnen Laibow-Koser >> marnen.org >> marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org >> > > I was able to successfully match the following cases: > > 300.00 > 123.00 > 123.11 > 300.01That''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.> > -ConradBest, -- Marnen Laibow-Koser marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via ruby-forum.com.
On Sat, Aug 8, 2009 at 7:05 AM, Marnen Laibow-Koser < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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.> > > Best, > -- > Marnen Laibow-Koser > marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted via ruby-forum.com. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Conrad Taylor wrote: [...]> validates_numericality_of only determines whether something is numericThat''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 marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via ruby-forum.com.
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!! -- Posted via ruby-forum.com. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at groups.google.com/group/rubyonrails-talk?hl=en.