[Best Practices] phone numbers?

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??

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.

On Tue, Jul 11, 2006, Larry K. 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 M. [email protected] wrote:

ways. Is there a

  1. or, is it better to store with formatting, ie.
    “(222) 111-2222” ?

  2. Is there a plugin/ class/ module that helps in
    dealing with phone
    numbers??


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

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 J.
E: [email protected]

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.

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

Ben J. 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

Apologies for double post.

Ben J. 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

Alan F. wrote:

Ben J. 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

Right 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.

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 F. wrote:

Ben J. 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs