andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-12 13:54 UTC
Validating no spaces in field
All, I''ve never been much of a regex wrangler, and learning rails at the same time isn''t helping. I want to create a validation that fails if a user tries to create a username with spaces. But I can''t seem to get the right validates_format_of regex going. Can anyone help with this simple question? Thanks! Andrew --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi, you can simply try yourString.include?(" ") with the params[:yourString] if it is a form parameter. Sayoyo andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> All, > > I''ve never been much of a regex wrangler, and learning rails at the > same time isn''t helping. > > I want to create a validation that fails if a user tries to create a > username with spaces. But I can''t seem to get the right > validates_format_of regex going. > > Can anyone help with this simple question? > > Thanks! > Andrew-- 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 -~----------~----~----~----~------~----~------~--~---
validates_format_of :username, :with => /^\S+$/ but that''s still probably too open for a username (it''ll allow "u $ern4m3" for instance) Most of the time, usernames are restricted to: validates_format_of :username, :with => /^[a-zA-Z][a-z0-9A-Z]{2,10}$/ (usernames starting with a letter, followed by 2 or more letters or digits up to 10, min username length = 3, max username length = 11) here are a few links that explain the syntax concisely: - http://www.math.hokudai.ac.jp/~gotoken/ruby/ruby-uguide/uguide05.html - http://www.rubyist.net/~slagell/ruby/regexp.html On Mar 12, 9:54 pm, "andrew.ohns...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <andrew.ohns...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> All, > > I''ve never been much of a regex wrangler, and learning rails at the > same time isn''t helping. > > I want to create a validation that fails if a user tries to create a > username with spaces. But I can''t seem to get the right > validates_format_of regex going. > > Can anyone help with this simple question? > > Thanks! > Andrew--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-13 01:06 UTC
Re: Validating no spaces in field
Thank you so much, that did the trick perfectly! And double thanks for teaching a man to fish. I think I might be able to write a simple regex of my own now! Thanks again!!! ---A On Mar 12, 1:27 pm, "eden li" <eden...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> validates_format_of :username, :with => /^\S+$/ > > but that''s still probably too open for a username (it''ll allow "u > $ern4m3" for instance) > > Most of the time, usernames are restricted to: > > validates_format_of :username, :with => /^[a-zA-Z][a-z0-9A-Z]{2,10}$/ > > (usernames starting with a letter, followed by 2 or more letters or > digits up to 10, min username length = 3, max username length = 11) > > here are a few links that explain the syntax concisely: > -http://www.math.hokudai.ac.jp/~gotoken/ruby/ruby-uguide/uguide05.html > -http://www.rubyist.net/~slagell/ruby/regexp.html > > On Mar 12, 9:54 pm, "andrew.ohns...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" > > <andrew.ohns...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > All, > > > I''ve never been much of a regex wrangler, and learning rails at the > > same time isn''t helping. > > > I want to create a validation that fails if a user tries to create a > > username with spaces. But I can''t seem to get the right > > validates_format_of regex going. > > > Can anyone help with this simple question? > > > Thanks! > > Andrew--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
While I think that your regex is smokin'' hot, I do think you might be better served [at least on the error messages front] by two validations there: validates_format_of :username, :with => /^[a-zA-Z][a-z0-9A-Z]*$/, :message => "can only contain letters and numbers" # Or something validates_length_of, :minimum => 2, :maximum => 10, :too_short => "must be at least 2 characters", :too_long => "cannot be longer than 10 characters" that way you''ll have separate error messages for each problem rather than having to lump them all in together. You way _does_ work though. I''m just trying to offer a helpful hint. RSL On 3/12/07, eden li <eden.li-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: validates_format_of :username, :with => /^[a-zA-Z][a-z0-9A-Z]{2,10}$/>--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-13 16:45 UTC
Re: Validating no spaces in field
Russell, I agree and my model already had a validates_length_of so I modified the proposed regex to eliminate the length check. Discussing this with my partner we decided to allow numbers, letters, and underscore (_) so I am going to try the regex /^[a-zA-Z0-9_]*$/ We don''t care if the first character is a letter, number, or underscore, only that these are the only characters allowed anywhere in the username. So "_username", "user_name", and "1user_name" should all be valid. Think this should work? I don''t have access to the site to test it until I get home tonight. I had never noticed that validates_length_of supports two error messages, one for too short, and one for too long. I''ll have to implement that in my code tonight as well. Now if I could only find a non-ugly way to sort the error messages so they match the order of the form fields I''d be all set. :( On Mar 13, 6:36 am, "Russell Norris" <sco...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> While I think that your regex is smokin'' hot, I do think you might be better > served [at least on the error messages front] by two validations there: > > validates_format_of :username, :with => /^[a-zA-Z][a-z0-9A-Z]*$/, :message > => "can only contain letters and numbers" # Or something > validates_length_of, :minimum => 2, :maximum => 10, :too_short => "must be > at least 2 characters", :too_long => "cannot be longer than 10 characters" > > that way you''ll have separate error messages for each problem rather than > having to lump them all in together. You way _does_ work though. I''m just > trying to offer a helpful hint. > > RSL > > On 3/12/07, eden li <eden...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > validates_format_of :username, :with => /^[a-zA-Z][a-z0-9A-Z]{2,10}$/ > > > > - Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
It''s not [that] hard to roll your own version of error_messages_on. Something like def error_messages_hacked(instance, *order) return if instance.errors.empty? dummy = order.map do |attribute| "#{instance} #{instance.errors_on(attribute)}" end.flatten # Do any HTML styling you want on dummy # which will be a collection of error message strings end <%= error_messages_hacked @user, :name, :email, :whatever -%> That''s pretty not ugly, right? On 3/13/07, andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Russell, > > I agree and my model already had a validates_length_of so I modified > the proposed regex to eliminate the length check. > > Discussing this with my partner we decided to allow numbers, letters, > and underscore (_) so I am going to try the regex > /^[a-zA-Z0-9_]*$/ > > We don''t care if the first character is a letter, number, or > underscore, only that these are the only characters allowed anywhere > in the username. So "_username", "user_name", and "1user_name" should > all be valid. > > Think this should work? I don''t have access to the site to test it > until I get home tonight. > > I had never noticed that validates_length_of supports two error > messages, one for too short, and one for too long. I''ll have to > implement that in my code tonight as well. > > Now if I could only find a non-ugly way to sort the error messages so > they match the order of the form fields I''d be all set. :( > > On Mar 13, 6:36 am, "Russell Norris" <sco...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > While I think that your regex is smokin'' hot, I do think you might be > better > > served [at least on the error messages front] by two validations there: > > > > validates_format_of :username, :with => /^[a-zA-Z][a-z0-9A-Z]*$/, > :message > > => "can only contain letters and numbers" # Or something > > validates_length_of, :minimum => 2, :maximum => 10, :too_short => "must > be > > at least 2 characters", :too_long => "cannot be longer than 10 > characters" > > > > that way you''ll have separate error messages for each problem rather > than > > having to lump them all in together. You way _does_ work though. I''m > just > > trying to offer a helpful hint. > > > > RSL > > > > On 3/12/07, eden li <eden...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > validates_format_of :username, :with => /^[a-zA-Z][a-z0-9A-Z]{2,10}$/ > > > > > > > > - Hide quoted text - > > > > - Show quoted text - > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---