Victor S
2011-Feb-18 05:24 UTC
validates_format_of :phone with => /^\([0-9]{3}\)[-. ]?[0-9]{3}[-. ]?[0-9]{4}|^[0-9]{3}[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/
Why doesn''t this phone validation work? validates_format_of :phone, :with => /^\([0-9]{3}\)[-. ]?[0-9]{3}[-. ]?[0-9]{4}|^[0-9]{3}[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/, :message => "is not a phone number" tested with (123) 456 7890 testing it here does work: http://www.rubular.com/ but in a rails app, it fails validation except for 1234567890 -- 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.
Colin Law
2011-Feb-18 08:17 UTC
Re: validates_format_of :phone with => /^\([0-9]{3}\)[-. ]?[0-9]{3}[-. ]?[0-9]{4}|^[0-9]{3}[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/
On 18 February 2011 05:24, Victor S <victor.stan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Why doesn''t this phone validation work? > validates_format_of :phone, :with => /^\([0-9]{3}\)[-. ]?[0-9]{3}[-. > ]?[0-9]{4}|^[0-9]{3}[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/, > :message => "is not a phone number" > tested with (123) 456 7890 > testing it here does work: http://www.rubular.com/ but in a rails app, it > fails validation except for 1234567890In these situations I dramatically simplify until I get an expression that does what I expect then build it back up to find the cause of the failure. Since you seem to suggest that it is not working when parentheses are included I would start with an expression that allows (n) and work up from there. Remember you can run it in the rails console so you do not need to keep editing the app. 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.
Victor S
2011-Feb-18 12:56 UTC
Re: validates_format_of :phone with => /^\([0-9]{3}\)[-. ]?[0-9]{3}[-. ]?[0-9]{4}|^[0-9]{3}[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/
I''ve gone through about a couple of hours of building it up and tearing it down, I need someone else''s brain on this one please... -- 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.
Victor S
2011-Feb-18 13:03 UTC
Re: validates_format_of :phone with => /^\([0-9]{3}\)[-. ]?[0-9]{3}[-. ]?[0-9]{4}|^[0-9]{3}[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/
To be a bit more clear about how I see the problem: as I said earlier, http://www.rubular.com/ validates the correctness of the expression, I''ve tried similar expressions in JS parsers, .NET parsers, Python parsers, they all pass, I think it''s the rails parsers that fails, and/or something about the validates with that doesn''t like the parenthesis... can someone prove me wrong? Please? -- 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.
Victor S
2011-Feb-18 13:18 UTC
Re: validates_format_of :phone with => /^\([0-9]{3}\)[-. ]?[0-9]{3}[-. ]?[0-9]{4}|^[0-9]{3}[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/
I have tested a simpler version and noticed this: ruby-1.9.2-p136 :010 > /^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/ =~ "1234567890" => 0 ruby-1.9.2-p136 :011 > /^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/ =~ "(123)4567890" => 0 ruby-1.9.2-p136 :012 > /^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/ =~ "(123) 456-7890" => 0 ruby-1.9.2-p136 :013 > /^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/ =~ "(123)3456-7890" => nil ruby-1.9.2-p136 :014 > /^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/ =~ "(123)-456-7890" => 0 ruby-1.9.2-p136 :015 > /^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/ =~ "(123) 456-7890 smurf" => nil ruby-1.9.2-p136 :016 > does 0 mean pass? And is then the validated_with not accepting 0 as a pass? -- 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.
Bill Walton
2011-Feb-18 13:27 UTC
Re: validates_format_of :phone with => /^\([0-9]{3}\)[-. ]?[0-9]{3}[-. ]?[0-9]{4}|^[0-9]{3}[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/
Hi Victor, On Fri, Feb 18, 2011 at 7:03 AM, Victor S <victor.stan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> To be a bit more clear about how I see the problem: as I said > earlier, http://www.rubular.com/ validates the correctness of the > expression, I''ve tried similar expressions in JS parsers, .NET parsers, > Python parsers, they all pass, I think it''s the rails parsers that fails, > and/or something about the validates with that doesn''t like the > parenthesis... can someone prove me wrong? Please?It might be that, in Ruby, ^$ match before / after newlines, not beginning / end of string. Give it a try with \A and \Z and see if that solves it. HTH, Billl -- 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.
Victor S
2011-Feb-18 13:37 UTC
Re: validates_format_of :phone with => /^\([0-9]{3}\)[-. ]?[0-9]{3}[-. ]?[0-9]{4}|^[0-9]{3}[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/
Bah! Much ado about nothing, I''m pretty sure now that the problem is that the database record only accepts integers for the phone column. I just checked the schema file, should have done that much earlier! -- 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.
Victor S
2011-Feb-18 13:42 UTC
Re: validates_format_of :phone with => /^\([0-9]{3}\)[-. ]?[0-9]{3}[-. ]?[0-9]{4}|^[0-9]{3}[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/
I do wish there was a different error message/exception raised if something like this happened rather than a failed validation, since validations ca be written to be independent of db column types and therefore not a reflection of what is acceptable to the database... -- 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.
Colin Law
2011-Feb-18 15:46 UTC
Re: validates_format_of :phone with => /^\([0-9]{3}\)[-. ]?[0-9]{3}[-. ]?[0-9]{4}|^[0-9]{3}[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/
On 18 February 2011 13:42, Victor S <victor.stan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I do wish there was a different error message/exception raised if something > like this happened rather than a failed validation, since validations ca be > written to be independent of db column types and therefore not a reflection > of what is acceptable to the database...In your OP you said that you were using the :message option on the validation. I would not have expected that message unless it was that validation that failed. You can specify a different message for each validation if you wish. 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.
Mark Kremer
2011-Feb-18 20:18 UTC
Re: validates_format_of :phone with => /^\([0-9]{3}\)[-. ]?[0-9]{3}[-. ]?[0-9]{4}|^[0-9]{3}[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/
While I am guessing you''ve already solved your problem (because it was related to your database schema) I''ll still answer your regular expression questions: The number returned is the position of the string where your regular expression matches, 0 is the start of the string. nil means no matches were found. In the case of your regular expression you''d only ever get 0 or nil because your regular expression requires the start of the string to match. On 18-2-2011 14:18, Victor S wrote:> I have tested a simpler version and noticed this: > > ruby-1.9.2-p136 :010 > /^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/ > =~ "1234567890" > => 0 > ruby-1.9.2-p136 :011 > /^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/ > =~ "(123)4567890" > => 0 > ruby-1.9.2-p136 :012 > /^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/ > =~ "(123) 456-7890" > => 0 > ruby-1.9.2-p136 :013 > /^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/ > =~ "(123)3456-7890" > => nil > ruby-1.9.2-p136 :014 > /^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/ > =~ "(123)-456-7890" > => 0 > ruby-1.9.2-p136 :015 > /^\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/ > =~ "(123) 456-7890 smurf" > => nil > ruby-1.9.2-p136 :016 > > > > does 0 mean pass? And is then the validated_with not accepting 0 as a > pass? > -- > 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.