I am having a really simple problem with regular expressions and Rails. I need to validate that input is a number or only digits. I using this: validates_format_of :po_box, :with => /\d*/, :message => "PO Box must be a number only. Remove all text." however, rails is letting anything be entered. i am getting other regular expressions like /a/ to work but for some reason not \d* or \d or [0-9]* or [0-9]. Can anyone help? thanks, Wes --~--~---------~--~----~------------~-------~--~----~ 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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Okay I found a validation method that works for this: validates_numericality_of ....to answer my own question. However, i still don''t understand why my approach above still doesn''t work??? Wes On Sep 18, 4:38 pm, Acroterra <acrote...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am having a really simple problem with regular expressions and > Rails. I need to validate that input is a number or only digits. I > using this: > > validates_format_of :po_box, > :with => /\d*/, > :message => "PO Box must be a number > only. Remove all text." > > however, rails is letting anything be entered. i am getting other > regular expressions like /a/ to work but for some reason not \d* or \d > or [0-9]* or [0-9]. > > Can anyone help? > thanks, > Wes--~--~---------~--~----~------------~-------~--~----~ 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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
/^[+\-]?\d+$/ is the expression to check if the format is an integer........ Wes On Sep 18, 4:56 pm, Acroterra <acrote...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Okay I found a validation method that works for this: > validates_numericality_of ....to answer my own question. However, i > still don''t understand why my approach above still doesn''t work??? > > Wes > > On Sep 18, 4:38 pm, Acroterra <acrote...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I am having a really simple problem with regular expressions and > > Rails. I need to validate that input is a number or only digits. I > > using this: > > > validates_format_of :po_box, > > :with => /\d*/, > > :message => "PO Box must be a number > > only. Remove all text." > > > however, rails is letting anything be entered. i am getting other > > regular expressions like /a/ to work but for some reason not \d* or \d > > or [0-9]* or [0-9]. > > > Can anyone help? > > thanks, > > Wes--~--~---------~--~----~------------~-------~--~----~ 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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You already answered your question, but for the record, /\d*/ matches zero or more occurrences of numeric digits, so entering anything meets the zero criterion. You can use \d+ which requires at least one digit, but more likely, you want /^\d+$/ which requires everything to be digits and requires an entry. HTH On Sep 18, 2007, at 3:38 PM, Acroterra wrote:> > I am having a really simple problem with regular expressions and > Rails. I need to validate that input is a number or only digits. I > using this: > > validates_format_of :po_box, > :with => /\d*/, > :message => "PO Box must be a number > only. Remove all text." > > however, rails is letting anything be entered. i am getting other > regular expressions like /a/ to work but for some reason not \d* or \d > or [0-9]* or [0-9]. > > Can anyone help? > thanks, > Wes > > > >--~--~---------~--~----~------------~-------~--~----~ 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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
/^[0-9]+$/ would work as well. -john On Sep 18, 2007, at 4:07 PM, Acroterra wrote:> > /^[+\-]?\d+$/ is the expression to check if the format is an > integer........ > > Wes > > > > On Sep 18, 4:56 pm, Acroterra <acrote...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Okay I found a validation method that works for this: >> validates_numericality_of ....to answer my own question. However, i >> still don''t understand why my approach above still doesn''t work??? >> >> Wes >> >> On Sep 18, 4:38 pm, Acroterra <acrote...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>> I am having a really simple problem with regular expressions and >>> Rails. I need to validate that input is a number or only digits. I >>> using this: >> >>> validates_format_of :po_box, >>> :with => /\d*/, >>> :message => "PO Box must be a number >>> only. Remove all text." >> >>> however, rails is letting anything be entered. i am getting other >>> regular expressions like /a/ to work but for some reason not \d* >>> or \d >>> or [0-9]* or [0-9]. >> >>> Can anyone help? >>> thanks, >>> Wes > > >--~--~---------~--~----~------------~-------~--~----~ 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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
John Adams wrote:> /^[0-9]+$/ would work as well. > > -johnin Regex the ^ Matches the starting position within the string. $ Matches the ending position of the string or the position just before a string-terminating newline. so /^ABC$/ matches ONLY the string ABC. and therefore /^\d+$/ requires everything to be digits just like steve said -- Posted via http://www.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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
No problem, we''ve said the same thing. :) --j On Sep 19, 2007, at 3:47 AM, Shai Rosenfeld wrote:> > John Adams wrote: >> /^[0-9]+$/ would work as well. >> >> -john > > in Regex the > > ^ Matches the starting position within the string. > $ Matches the ending position of the string or the position just > before a string-terminating newline. > > > so /^ABC$/ matches ONLY the string ABC. and therefore > /^\d+$/ requires everything to be digits just like steve said > -- > Posted via http://www.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 http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---