Obviously not a genius with Ruby.
I simply want to check if the string from an form input is a phone
number, or an email.
I started testing wether the string.class was Bignum or Fixnum. If it
was I presumed it was a phonenumber. Although this works in script/
console, my application dosn''t seem to pass the Bignum and Fixnum
tests and proceeds to the else statement.
So now I wanna try looking for the content in my string.
I thought, hey, ill just look for the "@" sign in the posted string.
But how? I''ve been searching for several hourse. And don''t
seem to run
into any solution.
So I''ll go to bed, and cross my fingers.
Thx in advance
Btw, this is what I''m trying:
I''m using a phonenumber or the email as login. Same field.
If the user enters his phonenumber, I wanna make sure it works weather
the user types:
0045xxxxxxxx
45xxxxxxxx
(0045) Danish area code.
def phone(input)
unless input == ############## <------ unless what? Was
thinking, unless @ in input.
value = input.to_s
lenStop = value.length
lenStart = value.length - 8
username = ''45'' + value[lenStart..lenStop]
return username
else
return input
end
end
I figure code dosn''t work if user puts spaces in his phonenumber. I
guess that can be fixed with a gsub " ", "".. something like
that.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
David, I believe that regular expressions is what you may be looking for..>> s = "(0045) 444-5555"=> "(0045) 444-5555">> codes = s.match /(\(\d+\))?\s*(\d{3})(\s*-\s*)?(\d{4})/=> #<MatchData "(0045) 444-5555" 1:"(0045)" 2:"444" 3:"-" 4:"5555">>> codes[0]=> "(0045) 444-5555">> codes[1]=> "(0045)">> codes[2]=> "444">> codes[4]=> "5555" hth.. David Liwoch wrote:> > I figure code dosn''t work if user puts spaces in his phonenumber. I > guess that can be fixed with a gsub " ", "".. something like that.-- 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 -~----------~----~----~----~------~----~------~--~---
On 9 Sep 2008, at 21:24, David Liwoch <quexistence-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Obviously not a genius with Ruby. > > I simply want to check if the string from an form input is a phone > number, or an email. >Read up on regular expressions Fred> I started testing wether the string.class was Bignum or Fixnum. If it > was I presumed it was a phonenumber. Although this works in script/ > console, my application dosn''t seem to pass the Bignum and Fixnum > tests and proceeds to the else statement. > > So now I wanna try looking for the content in my string. > > I thought, hey, ill just look for the "@" sign in the posted string. > But how? I''ve been searching for several hourse. And don''t seem to run > into any solution. > > So I''ll go to bed, and cross my fingers. > > Thx in advance > > Btw, this is what I''m trying: > > I''m using a phonenumber or the email as login. Same field. > If the user enters his phonenumber, I wanna make sure it works weather > the user types: > > 0045xxxxxxxx > 45xxxxxxxx > > (0045) Danish area code. > > > def phone(input) > unless input == ############## <------ unless what? Was > thinking, unless @ in input. > value = input.to_s > lenStop = value.length > lenStart = value.length - 8 > username = ''45'' + value[lenStart..lenStop] > > return username > else > return input > end > end > > I figure code dosn''t work if user puts spaces in his phonenumber. I > guess that can be fixed with a gsub " ", "".. something like that. > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
regular expressions on the menu today.
Cuz:
/(\(\d+\))?\s*(\d{3})(\s*-\s*)?(\d{4})/
Makes -200% sense to me currently
Thank you guys
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
the others are right to point you to regular expressions--definitely worth
learning.
But to give you a fish, you can use String''s [] method to check for an
@ symbol.
if my_string[''@''] then
# my_string has an @ in it.
end
HTH,
-Roy
-----Original Message-----
From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
[mailto:rubyonrails-talk@googlegroups.com] On Behalf Of David Liwoch
Sent: Tuesday, September 09, 2008 1:25 PM
To: Ruby on Rails: Talk
Subject: [Rails] HowTo search a string for content?
Obviously not a genius with Ruby.
I simply want to check if the string from an form input is a phone number, or
an email.
I started testing wether the string.class was Bignum or Fixnum. If it was I
presumed it was a phonenumber. Although this works in script/ console, my
application dosn''t seem to pass the Bignum and Fixnum tests and
proceeds to the else statement.
So now I wanna try looking for the content in my string.
I thought, hey, ill just look for the "@" sign in the posted string.
But how? I''ve been searching for several hourse. And don''t
seem to run into any solution.
So I''ll go to bed, and cross my fingers.
Thx in advance
Btw, this is what I''m trying:
I''m using a phonenumber or the email as login. Same field.
If the user enters his phonenumber, I wanna make sure it works weather the user
types:
0045xxxxxxxx
45xxxxxxxx
(0045) Danish area code.
def phone(input)
unless input == ############## <------ unless what? Was thinking,
unless @ in input.
value = input.to_s
lenStop = value.length
lenStart = value.length - 8
username = ''45'' + value[lenStart..lenStop]
return username
else
return input
end
end
I figure code dosn''t work if user puts spaces in his phonenumber. I
guess that can be fixed with a gsub " ", "".. something like
that.
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Hehe.. definetly a more simple way to do exactly that. Thank you Guess Reg Exp can probably define a more accurate pattern for email validation. Thx On Sep 10, 2:23 pm, "Pardee, Roy" <parde...-go57ItdSaco@public.gmane.org> wrote:> the others are right to point you to regular expressions--definitely worth learning. > > But to give you a fish, you can use String''s [] method to check for an @ symbol. > > if my_string[''@''] then > # my_string has an @ in it. > end > > HTH, > > -Roy > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of David Liwoch > Sent: Tuesday, September 09, 2008 1:25 PM > To: Ruby on Rails: Talk > Subject: [Rails]HowTosearch a string for content? > > Obviously not a genius with Ruby. > > I simply want to check if the string from an form input is a phone number, or an email. > > I started testing wether the string.class was Bignum or Fixnum. If it was I presumed it was a phonenumber. Although this works in script/ console, my application dosn''t seem to pass the Bignum and Fixnum tests and proceeds to the else statement. > > So now I wanna try looking for the content in my string. > > I thought, hey, ill just look for the "@" sign in the posted string. > But how? I''ve been searching for several hourse. And don''t seem to run into any solution. > > So I''ll go to bed, and cross my fingers. > > Thx in advance > > Btw, this is what I''m trying: > > I''m using a phonenumber or the email as login. Same field. > If the user enters his phonenumber, I wanna make sure it works weather the user types: > > 0045xxxxxxxx > 45xxxxxxxx > > (0045) Danish area code. > > def phone(input) > unless input == ############## <------ unless what? Was thinking, unless @ in input. > value = input.to_s > lenStop = value.length > lenStart = value.length - 8 > username = ''45'' + value[lenStart..lenStop] > > return username > else > return input > end > end > > I figure code dosn''t work if user puts spaces in his phonenumber. I guess that can be fixed with a gsub " ", "".. something like that. > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Heh--there''s actually a famous regexp (if you can imagine such a thing) that faithfully tracks the RFA spec for e-mail addresses. It''s something like half a printed page long and would make a kick-ass tattoo. I''m writing from the bus, else I''d google up a link to it. But lots of much much simpler regexps will handle 99% of valid addresses. Those are easily googled up... -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk@googlegroups.com] On Behalf Of David Liwoch Sent: Thursday, September 11, 2008 12:40 AM To: Ruby on Rails: Talk Subject: [Rails] Re: HowTo search a string for content? Hehe.. definetly a more simple way to do exactly that. Thank you Guess Reg Exp can probably define a more accurate pattern for email validation. Thx On Sep 10, 2:23 pm, "Pardee, Roy" <parde...-go57ItdSaco@public.gmane.org> wrote:> the others are right to point you to regular expressions--definitely worth learning. > > But to give you a fish, you can use String''s [] method to check for an @ symbol. > > if my_string[''@''] then > # my_string has an @ in it. > end > > HTH, > > -Roy > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of David Liwoch > Sent: Tuesday, September 09, 2008 1:25 PM > To: Ruby on Rails: Talk > Subject: [Rails]HowTosearch a string for content? > > Obviously not a genius with Ruby. > > I simply want to check if the string from an form input is a phone number, or an email. > > I started testing wether the string.class was Bignum or Fixnum. If it was I presumed it was a phonenumber. Although this works in script/ console, my application dosn''t seem to pass the Bignum and Fixnum tests and proceeds to the else statement. > > So now I wanna try looking for the content in my string. > > I thought, hey, ill just look for the "@" sign in the posted string. > But how? I''ve been searching for several hourse. And don''t seem to run into any solution. > > So I''ll go to bed, and cross my fingers. > > Thx in advance > > Btw, this is what I''m trying: > > I''m using a phonenumber or the email as login. Same field. > If the user enters his phonenumber, I wanna make sure it works weather the user types: > > 0045xxxxxxxx > 45xxxxxxxx > > (0045) Danish area code. > > def phone(input) > unless input == ############## <------ unless what? Was thinking, unless @ in input. > value = input.to_s > lenStop = value.length > lenStart = value.length - 8 > username = ''45'' + value[lenStart..lenStop] > > return username > else > return input > end > end > > I figure code dosn''t work if user puts spaces in his phonenumber. I guess that can be fixed with a gsub " ", "".. something like that. > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
David - On 11-Sep-08, at 11:13 AM, Pardee, Roy wrote:> > Heh--there''s actually a famous regexp (if you can imagine such a > thing) that faithfully tracks the RFA spec for e-mail addresses. > It''s something like half a printed page long and would make a kick- > ass tattoo. > > I''m writing from the bus, else I''d google up a link to it. > > But lots of much much simpler regexps will handle 99% of valid > addresses. Those are easily googled up... > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > ] On Behalf Of David Liwoch > Sent: Thursday, September 11, 2008 12:40 AM > To: Ruby on Rails: Talk > Subject: [Rails] Re: HowTo search a string for content? > > > Hehe.. definetly a more simple way to do exactly that. Thank you > > Guess Reg Exp can probably define a more accurate pattern for email > validation. > > Thxthis is one that works pretty well for us: validates_format_of :email, :with => /\b[A-Z0-9._%-]+@[A- Z0-9.-]+\.[A-Z]{2,4}\b/i, :if => Proc.new { |user| !user.email.blank? && !user.password_being_reset} it might have some holes Jodi --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---