I need to validate a string from a user (a location for use with geotagging) that is. I want to make sure that the user specify a city and a country in my inp ... So i wrote this => validates :usri_location, :presence => true, :format => {:with => /^[a-zA-Z]+{\,\s}+[a-zA-Z]/i}, :on => :create I want to make sure that the user writes city, country city + ", " + country but all i get is (invalid) some one with more experience with regex wanting to help out or show me in a direction. (spent the last hour trying code from http://www.regular-expressions.info/reference.html but i can''t get it to work ... /Niklas. -- 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 http://groups.google.com/group/rubyonrails-talk?hl=en.
On 12 August 2011 14:08, Niklas Nson <niklasnson-5ZBJkHaGu7EwFerOooGFRg@public.gmane.org> wrote:> I need to validate a string from a user (a location for use with > geotagging) that is. I want to make sure that the user specify a city > and a country in my inp ... So i wrote this => > > validates :usri_location, > :presence => true, > :format => {:with => /^[a-zA-Z]+{\,\s}+[a-zA-Z]/i}, > :on => :create > > I want to make sure that the user writes city, country > > city + ", " + country > > but all i get is (invalid) some one with more experience with regex > wanting to help out or show me in a direction.The way I tackle this sort of problem is to start with a simple regex and slowly build it up. So you could start with just expecting alpha characters and check that it accepts the city ok, then add the expected comma, and so on. Colin -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Aug 12, 2011, at 9:21 AM, Colin Law wrote:> On 12 August 2011 14:08, Niklas Nson <niklasnson-5ZBJkHaGu7EwFerOooGFRg@public.gmane.org> wrote: >> I need to validate a string from a user (a location for use with >> geotagging) that is. I want to make sure that the user specify a city >> and a country in my inp ... So i wrote this => >> >> validates :usri_location, >> :presence => true, >> :format => {:with => /^[a-zA-Z]+{\,\s}+[a-zA-Z]/i},You have {} instead of () around the , and \s. Also, I don''t believe the '','' has to be escaped (it didn''t work with it escaped and it does if i remove the escaping. With those 2 issues fixed it works for me on rubular.>> :on => :create >> >> I want to make sure that the user writes city, country >> >> city + ", " + country >> >> but all i get is (invalid) some one with more experience with regex >> wanting to help out or show me in a direction. > > The way I tackle this sort of problem is to start with a simple regex > and slowly build it up. So you could start with just expecting alpha > characters and check that it accepts the city ok, then add the > expected comma, and so on. > > Colin > > -- > 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 http://groups.google.com/group/rubyonrails-talk?hl=en. >Juan Alvarado -- 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 http://groups.google.com/group/rubyonrails-talk?hl=en.
Try using http://rubular.com/. I use this a lot because it''s fast (most of the time) and interactive. **Leigh>I need to validate a string from a user (a location for use with >geotagging) that is. I want to make sure that the user specify a city >and a country in my inp ... So i wrote this => > > validates :usri_location, > :presence => true, > :format => {:with => /^[a-zA-Z]+{\,\s}+[a-zA-Z]/i}, > :on => :create > >I want to make sure that the user writes city, country > >city + ", " + country > >but all i get is (invalid) some one with more experience with regex >wanting to help out or show me in a direction. >(spent the last hour trying code from http://www.regular- >expressions.info/reference.html >but i can''t get it to work ... > > >/Niklas. > >-- >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 http://groups.google.com/group/ >rubyonrails-talk?hl=en. >-- 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 http://groups.google.com/group/rubyonrails-talk?hl=en.
Juan Alvarado wrote in post #1016383:> On Aug 12, 2011, at 9:21 AM, Colin Law wrote: > >> On 12 August 2011 14:08, Niklas Nson <niklasnson-5ZBJkHaGu7EwFerOooGFRg@public.gmane.org> wrote: >>> I need to validate a string from a user (a location for use with >>> geotagging) that is. I want to make sure that the user specify a city >>> and a country in my inp ... So i wrote this => >>> >>> validates :usri_location, >>> :presence => true, >>> :format => {:with => /^[a-zA-Z]+{\,\s}+[a-zA-Z]/i}, > > You have {} instead of () around the , and \s. Also, I don''t believe > the '','' has to be escaped (it didn''t work with it escaped and it does if > i remove the escaping. With those 2 issues fixed it works for me on > rubular. >Also \s matches tabs and newlines, so your regex would match things like: xxxx, yyyy and xxxx, yyy so if you just want to match a space, then use a space in your regex: /[a-zA-Z]+, [a-zA-z]+/ -- 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-/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 http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks - this is a bookmark from now on... On 12 Aug, 15:35, Leigh Daniels <leighdaniel...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Try usinghttp://rubular.com/. I use this a lot because it''s fast (most of the time) and interactive. > > **Leigh > > > > > > > > >I need to validate a string from a user (a location for use with > >geotagging) that is. I want to make sure that the user specify a city > >and a country in my inp ... So i wrote this => > > > validates :usri_location, > > :presence => true, > > :format => {:with => /^[a-zA-Z]+{\,\s}+[a-zA-Z]/i}, > > :on => :create > > >I want to make sure that the user writes city, country > > >city + ", " + country > > >but all i get is (invalid) some one with more experience with regex > >wanting to help out or show me in a direction. > >(spent the last hour trying code fromhttp://www.regular- > >expressions.info/reference.html > >but i can''t get it to work ... > > >/Niklas. > > >-- > >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 athttp://groups.google.com/group/ > >rubyonrails-talk?hl=en.-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks for all help! On 12 Aug, 15:34, Juan Alvarado <jcalva...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 12, 2011, at 9:21 AM, Colin Law wrote: > > > On 12 August 2011 14:08, Niklas Nson <niklasn...-5ZBJkHaGu7EwFerOooGFRg@public.gmane.org> wrote: > >> I need to validate a string from a user (a location for use with > >> geotagging) that is. I want to make sure that the user specify a city > >> and a country in my inp ... So i wrote this => > > >> validates :usri_location, > >> :presence => true, > >> :format => {:with => /^[a-zA-Z]+{\,\s}+[a-zA-Z]/i}, > > You have {} instead of () around the , and \s. Also, I don''t believe the '','' has to be escaped (it didn''t work with it escaped and it does if i remove the escaping. With those 2 issues fixed it works for me on rubular. > > > > > > > > > > >> :on => :create > > >> I want to make sure that the user writes city, country > > >> city + ", " + country > > >> but all i get is (invalid) some one with more experience with regex > >> wanting to help out or show me in a direction. > > > The way I tackle this sort of problem is to start with a simple regex > > and slowly build it up. So you could start with just expecting alpha > > characters and check that it accepts the city ok, then add the > > expected comma, and so on. > > > Colin > > > -- > > 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 athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > Juan Alvarado-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks :) On 12 Aug, 16:19, 7stud -- <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Juan Alvarado wrote in post #1016383: > > > > > > > > > > > On Aug 12, 2011, at 9:21 AM, Colin Law wrote: > > >> On 12 August 2011 14:08, Niklas Nson <niklasn...-5ZBJkHaGu7EwFerOooGFRg@public.gmane.org> wrote: > >>> I need to validate a string from a user (a location for use with > >>> geotagging) that is. I want to make sure that the user specify a city > >>> and a country in my inp ... So i wrote this => > > >>> validates :usri_location, > >>> :presence => true, > >>> :format => {:with => /^[a-zA-Z]+{\,\s}+[a-zA-Z]/i}, > > > You have {} instead of () around the , and \s. Also, I don''t believe > > the '','' has to be escaped (it didn''t work with it escaped and it does if > > i remove the escaping. With those 2 issues fixed it works for me on > > rubular. > > Also \s matches tabs and newlines, so your regex would match things > like: > > xxxx, yyyy > > and > > xxxx, > yyy > > so if you just want to match a space, then use a space in your regex: > > /[a-zA-Z]+, [a-zA-z]+/ > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Just be aware that with that regular expression you are not allowing cities which names contains white spaces. New York, USA is not going to match. Also non standard ascii characters are not going to match (this could or could not be a problem depending on the language and city names for the countries you need to validate). Apart of the regular expression, if you must be sure the input correspond to a "city, country" string you could also have cities and countries tables and check that the input is in your tables. Otherwise a string like "AAA, AAA" will match. On Fri, Aug 12, 2011 at 12:46 PM, Niklas Nson <niklasnson-5ZBJkHaGu7EwFerOooGFRg@public.gmane.org>wrote:> Thanks :) > > On 12 Aug, 16:19, 7stud -- <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > > Juan Alvarado wrote in post #1016383: > > > > > > > > > > > > > > > > > > > > > On Aug 12, 2011, at 9:21 AM, Colin Law wrote: > > > > >> On 12 August 2011 14:08, Niklas Nson <niklasn...-5ZBJkHaGu7EwFerOooGFRg@public.gmane.org> wrote: > > >>> I need to validate a string from a user (a location for use with > > >>> geotagging) that is. I want to make sure that the user specify a city > > >>> and a country in my inp ... So i wrote this => > > > > >>> validates :usri_location, > > >>> :presence => true, > > >>> :format => {:with => /^[a-zA-Z]+{\,\s}+[a-zA-Z]/i}, > > > > > You have {} instead of () around the , and \s. Also, I don''t believe > > > the '','' has to be escaped (it didn''t work with it escaped and it does > if > > > i remove the escaping. With those 2 issues fixed it works for me on > > > rubular. > > > > Also \s matches tabs and newlines, so your regex would match things > > like: > > > > xxxx, yyyy > > > > and > > > > xxxx, > > yyy > > > > so if you just want to match a space, then use a space in your regex: > > > > /[a-zA-Z]+, [a-zA-z]+/ > > > > -- > > Posted viahttp://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-/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 > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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 http://groups.google.com/group/rubyonrails-talk?hl=en.