I''ve seen phone numbers handled many different ways. Is there a recommended Rails way to store, format and display phone numbers? I''d appreciate comments from the community on this. 1. Is it better to store phone number as a 10-digit string? Then format it as desired when displaying it? 2. or, is it better to store with formatting, ie. "(222) 111-2222" ? 3. Is there a plugin/ class/ module that helps in dealing with phone numbers?? -- Best Regards, -Larry "Work, work, work...there is no satisfactory alternative." --- E.Taft Benson -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060711/14d3052f/attachment.html
In one of my projects I''m storing them as 10 char strings (wanted to use :integer but it wasn''t big enough), and then using a method in the phone model to make them pretty again for display. I''d love to hear what others do though. Larry Kelly wrote:> I''ve seen phone numbers handled many different ways. Is there a > recommended > Rails way to store, format and display phone numbers? I''d appreciate > comments from the community on this. > > 1. Is it better to store phone number as a 10-digit string? Then > format it > as desired when displaying it? > > 2. or, is it better to store with formatting, ie. "(222) 111-2222" ? > > 3. Is there a plugin/ class/ module that helps in dealing with phone > numbers?? > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060711/9bd13402/attachment.html
On Tue, Jul 11, 2006, Larry Kelly wrote:> I''ve seen phone numbers handled many different ways. Is there a recommended > Rails way to store, format and display phone numbers? I''d appreciate > comments from the community on this. > > 1. Is it better to store phone number as a 10-digit string? Then format it > as desired when displaying it? > > 2. or, is it better to store with formatting, ie. "(222) 111-2222" ?Depends on your application. I''m currently working on an ecommerce site targeted at state agencies. They need to put in the phone number of someone who can authorize the order. In my situation, I store it as entered in a string field. I do validation by stripping out non-digit characters and making sure it''s at least 10 digits long. Since it will only ever be displayed to the user who entered it and the fulfillment department, this is fine for this app. If you were writing a phone book application, on the other hand, you''d want consistent display. In this case I''d suggest stripping out non-digit characters and formatting it on the way out. In my opinion, the most important thing is to let the user enter it any way they want. It''s easy to extract only the digits for any sort of validation/storage, but it''s a pain (for the user) to have arbitrary rules in place. Ben
What about extensions? (222) 123-4567 x123 What about letters? 1-800-1-CALL-ME --- Jonathan Motta <jmotta@wwidea.org> wrote:> In one of my projects I''m storing them as 10 char > strings (wanted to use > :integer but it wasn''t big enough), and then using > a method in the > phone model to make them pretty again for display. > I''d love to hear > what others do though. > > Larry Kelly wrote: > > I''ve seen phone numbers handled many different > ways. Is there a > > recommended > > Rails way to store, format and display phone > numbers? I''d appreciate > > comments from the community on this. > > > > 1. Is it better to store phone number as a > 10-digit string? Then > > format it > > as desired when displaying it? > > > > 2. or, is it better to store with formatting, ie. > "(222) 111-2222" ? > > > > 3. Is there a plugin/ class/ module that helps in > dealing with phone > > numbers?? > > > > > > >------------------------------------------------------------------------> > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Hi, I''m only throwing my opinion in rather than something more useful (like real life implementation) but there have been previous discussions about delivery addresses which overlap with this question. That discussion culminated in the general principal that the user knows best what address is going to get through to them and unless you are genuinely doing some sort of machine parsing (and most people aren''t) you should use a free format text field and let the user enter the information in whatever format they prefer. I would apply the same logic to phone numbers. Personally I prefer to enter phone numbers as: +61 417 123 456 ...because + is the international dial prefix, 61 is Australia and 417 123456 is the phone number. dialling that number in Australia I would enter 0417 123456. Having said that a user might enter: 00 11 61 417 123456 ...which is the Australian international dial prefix (0011) and that would less useful- in the U.S. the same thing would be 011 61 417 123456. So users can certainly enter unhelpful information. I would certainly capture the country of the user separately to the phone so you could cross check in case they enter a local phone number. The most important thing is to be clear what you are asking for- you want the International dial code for the country (61 for Australia, 1 for the US) but not International dial prefix for your provider (which could be 0011, 011 or lots of other things). Also, users often format the number for ease of (human) use. So: +61 417 417 417 +61 417 55 66 77 +612 216 612 0 These number are formatted in a human readable/usable way which is information that would be nice to preserve. I fall back on the general principle that the user knows what is going to get through to them and you should be be generous in what you accept. If you later need to machine parse it, make the computer do the hard work, not the user. Jeremy.> On Tue, Jul 11, 2006, Larry Kelly wrote: > >> I''ve seen phone numbers handled many different ways. Is there a recommended >> Rails way to store, format and display phone numbers? I''d appreciate >> comments from the community on this. >> >> 1. Is it better to store phone number as a 10-digit string? Then format it >> as desired when displaying it? >> >> 2. or, is it better to store with formatting, ie. "(222) 111-2222" ? >>-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060712/79f58e30/attachment.html
The database is generally there to store data. You should store the phone numbers raw: 2222222222 and then format it as you display it. Because what if you want to change the formatting later? If you want to keep the formatting consistent just use a helper function or something. Hope that helps. Thank You, Ben Johnson E: bjohnson@contuitive.com On Jul 12, 2006, at 1:11 AM, Jeremy Nelson wrote:> Hi, > > I''m only throwing my opinion in rather than something more useful > (like real life implementation) but there have been previous > discussions about delivery addresses which overlap with this > question. That discussion culminated in the general principal that > the user knows best what address is going to get through to them > and unless you are genuinely doing some sort of machine parsing > (and most people aren''t) you should use a free format text field > and let the user enter the information in whatever format they prefer. > > I would apply the same logic to phone numbers. Personally I prefer > to enter phone numbers as: > > +61 417 123 456 > > ...because + is the international dial prefix, 61 is Australia and > 417 123456 is the phone number. dialling that number in Australia I > would enter 0417 123456. > > Having said that a user might enter: > > 00 11 61 417 123456 > > ...which is the Australian international dial prefix (0011) and > that would less useful- in the U.S. the same thing would be 011 61 > 417 123456. So users can certainly enter unhelpful information. I > would certainly capture the country of the user separately to the > phone so you could cross check in case they enter a local phone > number. > > The most important thing is to be clear what you are asking for- > you want the International dial code for the country (61 for > Australia, 1 for the US) but not International dial prefix for your > provider (which could be 0011, 011 or lots of other things). > > Also, users often format the number for ease of (human) use. So: > > +61 417 417 417 > +61 417 55 66 77 > +612 216 612 0 > > These number are formatted in a human readable/usable way which is > information that would be nice to preserve. > > I fall back on the general principle that the user knows what is > going to get through to them and you should be be generous in what > you accept. If you later need to machine parse it, make the > computer do the hard work, not the user. > > Jeremy. > >> On Tue, Jul 11, 2006, Larry Kelly wrote: >> >>> I''ve seen phone numbers handled many different ways. Is there a >>> recommended >>> Rails way to store, format and display phone numbers? I''d >>> appreciate >>> comments from the community on this. >>> >>> 1. Is it better to store phone number as a 10-digit string? Then >>> format it >>> as desired when displaying it? >>> >>> 2. or, is it better to store with formatting, ie. "(222) >>> 111-2222" ? >>> > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060712/978d7b75/attachment-0001.html
I agree with Jeremy, in most cases is most practical just to store the number as entered. There is no "best" way to do it, as it depends on your application needs. If you are just capturing a customer''s phone number so that you can contact him later, then it''s sensible to capture as entered, especially if you are dealing with international numbers. Let''s be pragmatic - is it worth your while to deal with parsing all the possible formats when all you need is to be able to have a user look at the number and dial it later? On the other hand, if you are writing a VoIP app or something that requires a strict number format, then it makes more sense to store the data as raw numbers. Ken Jeremy Nelson wrote:> Hi, > > I''m only throwing my opinion in rather than something more useful > (like real life implementation) but there have been previous > discussions about delivery addresses which overlap with this question. > That discussion culminated in the general principal that the user > knows best what address is going to get through to them and unless you > are genuinely doing some sort of machine parsing (and most people > aren''t) you should use a free format text field and let the user enter > the information in whatever format they prefer. > > I would apply the same logic to phone numbers. Personally I prefer to > enter phone numbers as: > > +61 417 123 456 > > ...because + is the international dial prefix, 61 is Australia and 417 > 123456 is the phone number. dialling that number in Australia I would > enter 0417 123456. > > Having said that a user might enter: > > 00 11 61 417 123456 > > ...which is the Australian international dial prefix (0011) and that > would less useful- in the U.S. the same thing would be 011 61 417 > 123456. So users can certainly enter unhelpful information. I would > certainly capture the country of the user separately to the phone so > you could cross check in case they enter a local phone number. > > The most important thing is to be clear what you are asking for- you > want the International dial code for the country (61 for Australia, 1 > for the US) but not International dial prefix for your provider (which > could be 0011, 011 or lots of other things). > > Also, users often format the number for ease of (human) use. So: > > +61 417 417 417 > +61 417 55 66 77 > +612 216 612 0 > > These number are formatted in a human readable/usable way which is > information that would be nice to preserve. > > I fall back on the general principle that the user knows what is going > to get through to them and you should be be generous in what you > accept. If you later need to machine parse it, make the computer do > the hard work, not the user. > > Jeremy. > >> On Tue, Jul 11, 2006, Larry Kelly wrote: >> >>> I''ve seen phone numbers handled many different ways. Is there a recommended >>> Rails way to store, format and display phone numbers? I''d appreciate >>> comments from the community on this. >>> >>> 1. Is it better to store phone number as a 10-digit string? Then format it >>> as desired when displaying it? >>> >>> 2. or, is it better to store with formatting, ie. "(222) 111-2222" ? >>> > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Ben Johnson wrote:> The database is generally there to store data. You should store the > phone numbers raw: 2222222222 and then format it as you display it. > Because what if you want to change the formatting later? If you want > to keep the formatting consistent just use a helper function or > something.Hmmm. UK numbers are 11 digits locally, but for international purposes we drop the leading 0 and have a with a 2 digit country code (44). +441231231234>From what I read aboce (I dont actually know) US numbers are 10 digits,with a 1 digit country code +11231231234 In both cases (+) stands in for you localities international dial code. For me in the UK thats 00, for the US it''s 011 etc etc. Formatting those numbers consistently after the fact would be hard. I think I''d go for storing some kind of formatting, or separate fields for country, area etc. Alan -- Posted via http://www.ruby-forum.com/.
Ben Johnson wrote:> The database is generally there to store data. You should store the > phone numbers raw: 2222222222 and then format it as you display it. > Because what if you want to change the formatting later? If you want > to keep the formatting consistent just use a helper function or > something.Hmmm. That works if you only care about one country. UK numbers are 11 digits locally, but for international purposes we drop the leading 0 and have a with a 2 digit country code (44). +441231231234>From what I read above (I don''t actually know) US numbers are 10 digits,with a 1 digit country code +11231231234 In both cases (+) stands in for you localities international dial code. For me in the UK thats 00, for the US it''s 011 etc etc. Formatting those numbers consistently after the fact would be hard. I think I''d go for storing some kind of formatting, or separate fields for country, area etc. Alan -- Posted via http://www.ruby-forum.com/.
Apologies for double post. -- Posted via http://www.ruby-forum.com/.
Sounds like a chance for an expert to create a phone number class (plugin, etc.) that we can all use...maybe with a simple parameter like country to drive the output formatting. -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org]On Behalf Of Kenneth Liu Sent: Wednesday, July 12, 2006 3:57 AM To: rails@lists.rubyonrails.org Subject: Re: [Rails] [Best Practices] phone numbers?? I agree with Jeremy, in most cases is most practical just to store the number as entered. There is no "best" way to do it, as it depends on your application needs. If you are just capturing a customer''s phone number so that you can contact him later, then it''s sensible to capture as entered, especially if you are dealing with international numbers. Let''s be pragmatic - is it worth your while to deal with parsing all the possible formats when all you need is to be able to have a user look at the number and dial it later? On the other hand, if you are writing a VoIP app or something that requires a strict number format, then it makes more sense to store the data as raw numbers. Ken Jeremy Nelson wrote:> Hi, > > I''m only throwing my opinion in rather than something more useful > (like real life implementation) but there have been previous > discussions about delivery addresses which overlap with this question. > That discussion culminated in the general principal that the user > knows best what address is going to get through to them and unless you > are genuinely doing some sort of machine parsing (and most people > aren''t) you should use a free format text field and let the user enter > the information in whatever format they prefer. > > I would apply the same logic to phone numbers. Personally I prefer to > enter phone numbers as: > > +61 417 123 456 > > ...because + is the international dial prefix, 61 is Australia and 417 > 123456 is the phone number. dialling that number in Australia I would > enter 0417 123456. > > Having said that a user might enter: > > 00 11 61 417 123456 > > ...which is the Australian international dial prefix (0011) and that > would less useful- in the U.S. the same thing would be 011 61 417 > 123456. So users can certainly enter unhelpful information. I would > certainly capture the country of the user separately to the phone so > you could cross check in case they enter a local phone number. > > The most important thing is to be clear what you are asking for- you > want the International dial code for the country (61 for Australia, 1 > for the US) but not International dial prefix for your provider (which > could be 0011, 011 or lots of other things). > > Also, users often format the number for ease of (human) use. So: > > +61 417 417 417 > +61 417 55 66 77 > +612 216 612 0 > > These number are formatted in a human readable/usable way which is > information that would be nice to preserve. > > I fall back on the general principle that the user knows what is going > to get through to them and you should be be generous in what you > accept. If you later need to machine parse it, make the computer do > the hard work, not the user. > > Jeremy. > >> On Tue, Jul 11, 2006, Larry Kelly wrote: >> >>> I''ve seen phone numbers handled many different ways. Is there a recommended >>> Rails way to store, format and display phone numbers? I''d appreciate >>> comments from the community on this. >>> >>> 1. Is it better to store phone number as a 10-digit string? Then format it >>> as desired when displaying it? >>> >>> 2. or, is it better to store with formatting, ie. "(222) 111-2222" ? >>> > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks for all the replies. My conclusions are these: 1. Users make mistakes, and don''t always format the entries the same way. ie. (253)111-2222 vs 253/111-2222 vs 253-111-2222 or ever (253) 111-2222 x48996 { for US phone nbrs}. The inconsistancy bothers me. 2. Pragmatically speaking. It is worth the effort control to validate the phone numbers? In this case, not really. Humans will be able to read them. That''s what matters most. Agile Programming methodology says don''t implement more than you need to, (or something like that). 3. A plugin would be nice. Something to look into when I have time. Unless someone beats me to it :) -Larry On 7/12/06, Nathan Leach <nathan.leach@phifer.com> wrote:> > Sounds like a chance for an expert to create a phone number class (plugin, > etc.) that we can all use...maybe with a simple parameter like country to > drive the output formatting. > > -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org]On Behalf Of Kenneth Liu > Sent: Wednesday, July 12, 2006 3:57 AM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] [Best Practices] phone numbers?? > > > I agree with Jeremy, in most cases is most practical just to store the > number as entered. There is no "best" way to do it, as it depends on > your application needs. If you are just capturing a customer''s phone > number so that you can contact him later, then it''s sensible to capture > as entered, especially if you are dealing with international numbers. > Let''s be pragmatic - is it worth your while to deal with parsing all the > possible formats when all you need is to be able to have a user look at > the number and dial it later? > > On the other hand, if you are writing a VoIP app or something that > requires a strict number format, then it makes more sense to store the > data as raw numbers. > > Ken > > Jeremy Nelson wrote: > > Hi, > > > > I''m only throwing my opinion in rather than something more useful > > (like real life implementation) but there have been previous > > discussions about delivery addresses which overlap with this question. > > That discussion culminated in the general principal that the user > > knows best what address is going to get through to them and unless you > > are genuinely doing some sort of machine parsing (and most people > > aren''t) you should use a free format text field and let the user enter > > the information in whatever format they prefer. > > > > I would apply the same logic to phone numbers. Personally I prefer to > > enter phone numbers as: > > > > +61 417 123 456 > > > > ...because + is the international dial prefix, 61 is Australia and 417 > > 123456 is the phone number. dialling that number in Australia I would > > enter 0417 123456. > > > > Having said that a user might enter: > > > > 00 11 61 417 123456 > > > > ...which is the Australian international dial prefix (0011) and that > > would less useful- in the U.S. the same thing would be 011 61 417 > > 123456. So users can certainly enter unhelpful information. I would > > certainly capture the country of the user separately to the phone so > > you could cross check in case they enter a local phone number. > > > > The most important thing is to be clear what you are asking for- you > > want the International dial code for the country (61 for Australia, 1 > > for the US) but not International dial prefix for your provider (which > > could be 0011, 011 or lots of other things). > > > > Also, users often format the number for ease of (human) use. So: > > > > +61 417 417 417 > > +61 417 55 66 77 > > +612 216 612 0 > > > > These number are formatted in a human readable/usable way which is > > information that would be nice to preserve. > > > > I fall back on the general principle that the user knows what is going > > to get through to them and you should be be generous in what you > > accept. If you later need to machine parse it, make the computer do > > the hard work, not the user. > > > > Jeremy. > > > >> On Tue, Jul 11, 2006, Larry Kelly wrote: > >> > >>> I''ve seen phone numbers handled many different ways. Is there a > recommended > >>> Rails way to store, format and display phone numbers? I''d appreciate > >>> comments from the community on this. > >>> > >>> 1. Is it better to store phone number as a 10-digit string? Then > format it > >>> as desired when displaying it? > >>> > >>> 2. or, is it better to store with formatting, ie. "(222) 111-2222" ? > >>> > > > > ------------------------------------------------------------------------ > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Best Regards, -Larry "Work, work, work...there is no satisfactory alternative." --- E.Taft Benson -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060712/859bc46c/attachment.html
Customers really like putting in numbers THEIR way. So, store what they enter for view and editing, as well as a normalized version to operate on internally. On Jul 12, 2006, at 10:33 AM, Larry Kelly wrote:> Thanks for all the replies. My conclusions are these: > > 1. Users make mistakes, and don''t always format the entries the > same way. ie. (253)111-2222 vs 253/111-2222 vs 253-111-2222 or > ever (253) 111-2222 x48996 { for US phone nbrs}. The > inconsistancy bothers me. > > 2. Pragmatically speaking. It is worth the effort control to > validate the phone numbers? In this case, not really. Humans will > be able to read them. That''s what matters most. Agile Programming > methodology says don''t implement more than you need to, (or > something like that). > > 3. A plugin would be nice. Something to look into when I have > time. Unless someone beats me to it :) > -Larry > > On 7/12/06, Nathan Leach < nathan.leach@phifer.com> wrote:Sounds > like a chance for an expert to create a phone number class (plugin, > etc.) that we can all use...maybe with a simple parameter like > country to drive the output formatting. > > -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org ]On Behalf Of Kenneth Liu > Sent: Wednesday, July 12, 2006 3:57 AM > To: rails@lists.rubyonrails.org > Subject: Re: [Rails] [Best Practices] phone numbers?? > > > I agree with Jeremy, in most cases is most practical just to store the > number as entered. There is no "best" way to do it, as it depends on > your application needs. If you are just capturing a customer''s phone > number so that you can contact him later, then it''s sensible to > capture > as entered, especially if you are dealing with international numbers. > Let''s be pragmatic - is it worth your while to deal with parsing > all the > possible formats when all you need is to be able to have a user > look at > the number and dial it later? > > On the other hand, if you are writing a VoIP app or something that > requires a strict number format, then it makes more sense to store the > data as raw numbers. > > Ken > > Jeremy Nelson wrote: > > Hi, > > > > I''m only throwing my opinion in rather than something more useful > > (like real life implementation) but there have been previous > > discussions about delivery addresses which overlap with this > question. > > That discussion culminated in the general principal that the user > > knows best what address is going to get through to them and > unless you > > are genuinely doing some sort of machine parsing (and most people > > aren''t) you should use a free format text field and let the user > enter > > the information in whatever format they prefer. > > > > I would apply the same logic to phone numbers. Personally I > prefer to > > enter phone numbers as: > > > > +61 417 123 456 > > > > ...because + is the international dial prefix, 61 is Australia > and 417 > > 123456 is the phone number. dialling that number in Australia I > would > > enter 0417 123456. > > > > Having said that a user might enter: > > > > 00 11 61 417 123456 > > > > ...which is the Australian international dial prefix (0011) and that > > would less useful- in the U.S. the same thing would be 011 61 417 > > 123456. So users can certainly enter unhelpful information. I would > > certainly capture the country of the user separately to the phone so > > you could cross check in case they enter a local phone number. > > > > The most important thing is to be clear what you are asking for- you > > want the International dial code for the country (61 for > Australia, 1 > > for the US) but not International dial prefix for your provider > (which > > could be 0011, 011 or lots of other things). > > > > Also, users often format the number for ease of (human) use. So: > > > > +61 417 417 417 > > +61 417 55 66 77 > > +612 216 612 0 > > > > These number are formatted in a human readable/usable way which is > > information that would be nice to preserve. > > > > I fall back on the general principle that the user knows what is > going > > to get through to them and you should be be generous in what you > > accept. If you later need to machine parse it, make the computer do > > the hard work, not the user. > > > > Jeremy. > > > >> On Tue, Jul 11, 2006, Larry Kelly wrote: > >> > >>> I''ve seen phone numbers handled many different ways. Is there a > recommended > >>> Rails way to store, format and display phone numbers? I''d > appreciate > >>> comments from the community on this. > >>> > >>> 1. Is it better to store phone number as a 10-digit string? > Then format it > >>> as desired when displaying it? > >>> > >>> 2. or, is it better to store with formatting, ie. "(222) > 111-2222" ? > >>> > > > > > ---------------------------------------------------------------------- > -- > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > -- > Best Regards, > -Larry > "Work, work, work...there is no satisfactory alternative." > --- E.Taft Benson > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Wed, Jul 12, 2006, Larry Kelly wrote:> Thanks for all the replies. My conclusions are these: > > 1. Users make mistakes, and don''t always format the entries the same way. > ie. (253)111-2222 vs 253/111-2222 vs 253-111-2222 or ever (253) 111-2222 > x48996 { for US phone nbrs}. The inconsistancy bothers me.Yeah, you have to get over that. Just let them enter whatever they like, run your validation, and store it as-entered. Or do crazy munging, it''s up to you :)> 2. Pragmatically speaking. It is worth the effort control to validate the > phone numbers? In this case, not really. Humans will be able to read > them. That''s what matters most. Agile Programming methodology says don''t > implement more than you need to, (or something like that).It''s easy to validate US number. Strip out everything that''s not a digit, strip off leading 0s and 1s, and make sure the result is at least 10 digits long. I''d say writing those two regular expressions is worth it ;) Ben
(joining this conversation late) I have found the following to effective: 1. allow users to enter data the way they want (support multiple methods) 2. validate all formats you support (add them as users require) 3. normalize the data before insert (update) 4. on retrieve, standardize the data (good place for internationalization) All work is done in the model. (this code was previously posted, and criticized for being not factored enough. feel free to implement the concept as you feel it requires. grin) Hopefully you can use this pattern - Works well for postalcodes (zips), dates, and other key High Value data. class Contact < ActiveRecord::Base protected #validate the phone formats accepted def validate if errors.on("phone").nil? validate_phone = self.phone_before_type_cast.gsub(/[^0-9]/,"") if !validate_phone.nil? and validate_phone.length != 7 and validate_phone.length != 10 errors.add("phone", "number is not correct. Either a 7 or 10 digit phone # must be specified.") end end end #format the phone number for display def after_find self.phone = "#{self.phone_before_type_cast[0..2]}-# {self.phone_before_type_cast[3..6]}" if !self.phone.nil? and self.phone_before_type_cast.length == 7 self.phone = "(#{self.phone_before_type_cast[0..2]}) # {self.phone_before_type_cast[3..5]}-#{self.phone_before_type_cast [6..9]}" if !self.phone.nil? and self.phone_before_type_cast.length == 10 end #remove all non-numbers def before_save self.phone_before_type_cast.gsub!(/[^0-9]/,"") if !self.phone.nil? end ... end On 12-Jul-06, at 1:39 PM, Tom Mornini wrote:> Customers really like putting in numbers THEIR way. > > So, store what they enter for view and editing, as > well as a normalized version to operate on internally. > > On Jul 12, 2006, at 10:33 AM, Larry Kelly wrote: > >> Thanks for all the replies. My conclusions are these: >> >> 1. Users make mistakes, and don''t always format the entries the >> same way. ie. (253)111-2222 vs 253/111-2222 vs 253-111-2222 or >> ever (253) 111-2222 x48996 { for US phone nbrs}. The >> inconsistancy bothers me. >> >> 2. Pragmatically speaking. It is worth the effort control to >> validate the phone numbers? In this case, not really. Humans >> will be able to read them. That''s what matters most. Agile >> Programming methodology says don''t implement more than you need >> to, (or something like that). >> >> 3. A plugin would be nice. Something to look into when I have >> time. Unless someone beats me to it :) >> -Larry >> >> On 7/12/06, Nathan Leach < nathan.leach@phifer.com> wrote:Sounds >> like a chance for an expert to create a phone number class >> (plugin, etc.) that we can all use...maybe with a simple parameter >> like country to drive the output formatting. >> >> -----Original Message----- >> From: rails-bounces@lists.rubyonrails.org >> [mailto:rails-bounces@lists.rubyonrails.org ]On Behalf Of Kenneth Liu >> Sent: Wednesday, July 12, 2006 3:57 AM >> To: rails@lists.rubyonrails.org >> Subject: Re: [Rails] [Best Practices] phone numbers?? >> >> >> I agree with Jeremy, in most cases is most practical just to store >> the >> number as entered. There is no "best" way to do it, as it depends on >> your application needs. If you are just capturing a customer''s phone >> number so that you can contact him later, then it''s sensible to >> capture >> as entered, especially if you are dealing with international numbers. >> Let''s be pragmatic - is it worth your while to deal with parsing >> all the >> possible formats when all you need is to be able to have a user >> look at >> the number and dial it later? >> >> On the other hand, if you are writing a VoIP app or something that >> requires a strict number format, then it makes more sense to store >> the >> data as raw numbers. >> >> Ken >> >> Jeremy Nelson wrote: >> > Hi, >> > >> > I''m only throwing my opinion in rather than something more useful >> > (like real life implementation) but there have been previous >> > discussions about delivery addresses which overlap with this >> question. >> > That discussion culminated in the general principal that the user >> > knows best what address is going to get through to them and >> unless you >> > are genuinely doing some sort of machine parsing (and most people >> > aren''t) you should use a free format text field and let the user >> enter >> > the information in whatever format they prefer. >> > >> > I would apply the same logic to phone numbers. Personally I >> prefer to >> > enter phone numbers as: >> > >> > +61 417 123 456 >> > >> > ...because + is the international dial prefix, 61 is Australia >> and 417 >> > 123456 is the phone number. dialling that number in Australia I >> would >> > enter 0417 123456. >> > >> > Having said that a user might enter: >> > >> > 00 11 61 417 123456 >> > >> > ...which is the Australian international dial prefix (0011) and >> that >> > would less useful- in the U.S. the same thing would be 011 61 417 >> > 123456. So users can certainly enter unhelpful information. I would >> > certainly capture the country of the user separately to the >> phone so >> > you could cross check in case they enter a local phone number. >> > >> > The most important thing is to be clear what you are asking for- >> you >> > want the International dial code for the country (61 for >> Australia, 1 >> > for the US) but not International dial prefix for your provider >> (which >> > could be 0011, 011 or lots of other things). >> > >> > Also, users often format the number for ease of (human) use. So: >> > >> > +61 417 417 417 >> > +61 417 55 66 77 >> > +612 216 612 0 >> > >> > These number are formatted in a human readable/usable way which is >> > information that would be nice to preserve. >> > >> > I fall back on the general principle that the user knows what is >> going >> > to get through to them and you should be be generous in what you >> > accept. If you later need to machine parse it, make the computer do >> > the hard work, not the user. >> > >> > Jeremy. >> > >> >> On Tue, Jul 11, 2006, Larry Kelly wrote: >> >> >> >>> I''ve seen phone numbers handled many different ways. Is there >> a recommended >> >>> Rails way to store, format and display phone numbers? I''d >> appreciate >> >>> comments from the community on this. >> >>> >> >>> 1. Is it better to store phone number as a 10-digit string? >> Then format it >> >>> as desired when displaying it? >> >>> >> >>> 2. or, is it better to store with formatting, ie. "(222) >> 111-2222" ? >> >>> >> > >> > >> --------------------------------------------------------------------- >> --- >> > >> > _______________________________________________ >> > Rails mailing list >> > Rails@lists.rubyonrails.org >> > http://lists.rubyonrails.org/mailman/listinfo/rails >> > >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> >> -- >> Best Regards, >> -Larry >> "Work, work, work...there is no satisfactory alternative." >> --- E.Taft Benson >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
>> 1. Users make mistakes, and don''t always format the entries the same way. >> ie. (253)111-2222 vs 253/111-2222 vs 253-111-2222 or ever (253) 111-2222 >> x48996 { for US phone nbrs}. The inconsistancy bothers me. > > Yeah, you have to get over that. Just let them enter whatever they > like, run your validation, and store it as-entered. Or do crazy > munging, it''s up to you :) > >> 2. Pragmatically speaking. It is worth the effort control to validate the >> phone numbers? In this case, not really. Humans will be able to read >> them. That''s what matters most. Agile Programming methodology says don''t >> implement more than you need to, (or something like that). > > It''s easy to validate US number. Strip out everything that''s not a > digit, strip off leading 0s and 1s, and make sure the result is at least > 10 digits long. I''d say writing those two regular expressions is worth > it ;)My old phone number would pass.... 352-2554 x204, but you still have no idea what the area code is... just something to keep in mind...
Alan Francis wrote:> Ben Johnson wrote: >> The database is generally there to store data. You should store the >> phone numbers raw: 2222222222 and then format it as you display it. >> Because what if you want to change the formatting later? If you want >> to keep the formatting consistent just use a helper function or >> something. > > Hmmm. That works if you only care about one country. UK numbers are > 11 digits locally, but for international purposes we drop the leading 0 > and have a with a 2 digit country code (44). > > +441231231234 > > From what I read above (I don''t actually know) US numbers are 10 digits, > with a 1 digit country code > > +11231231234 > > In both cases (+) stands in for you localities international dial code. > For me in the UK thats 00, for the US it''s 011 etc etc. > > Formatting those numbers consistently after the fact would be hard. I > think I''d go for storing some kind of formatting, or separate fields for > country, area etc. > > AlanRight but formatting is has no place in a database. If you are worried about different countries should add a phone_location field or something. You still should store the phone number as raw data, whether it be +2222222222222 or +#g%hgf%54HGF or 4444444444 or whothehellcares, then in your helper function format it depending on phone_location. -- Posted via http://www.ruby-forum.com/.
Accept anything and extract the digis. Typical US users (with their insanely geo-centric view) will never enter their country code because they rarely remember that the world holds other countries. Likewise, lazy sites try and force users to enter phone numbers in a specific format (e.g. "(###)###-####") or no non-numeric characters at all. Parse it as best you can. If it is ambiguous (e.g. seven-digit numbers can be local US phone numbers without area code, or Brasil), show them what you think it might be and let them pick or enter it unambiguously. Cheers Alan Francis wrote:> Ben Johnson wrote: >> The database is generally there to store data. You should store the >> phone numbers raw: 2222222222 and then format it as you display it.> From what I read aboce (I dont actually know) US numbers are 10 digits, > with a 1 digit country code > > +11231231234 > > In both cases (+) stands in for you localities international dial code. > For me in the UK thats 00, for the US it''s 011 etc etc.-- Posted via http://www.ruby-forum.com/.