Is it better to store telephone numbers in some canonical format or “as entered”?

前端 未结 12 1469
鱼传尺愫
鱼传尺愫 2020-12-11 00:59

Storing a telehone number in some kind of canonical format has several advantages from a programmers point of view, but it might confuse the user, if sudde

相关标签:
12条回答
  • 2020-12-11 01:37

    Hopefully this is a more practical and applied answer to an old question.

    Take a look at https://github.com/googlei18n/libphonenumber.

    As @Gumbo alluded to, I would store the phone number as E.164, which the above library parses for you. It can be used from several different programming languages.

    For DB storage you could in fact use E.164 as Base64 (since it ironically is valid base64), and decode Base64 as bytes. I believe the number of bytes from a string like that will fit a standard long. Personally I would just store the E.164 as a string in the database though.

    Of course, you should probably also store what the user entered originally before parsing, but I highly recommend you enter in some canonical number like E.164 for future integration with other systems.

    0 讨论(0)
  • 2020-12-11 01:40

    My gut instinct is to canonicalize according to the local standards of the entity, then render in the canonical representation modulus usability.

    0 讨论(0)
  • 2020-12-11 01:46

    Validate the input but allow wide array of formats. Store it as user typed it and then reformat the output as needed.

    Let's say user typed his number during registration to public phonebook application. So I would display it 'as user typed it' in the textfield on his 'edit my profile' page, for example. But I would display it reformatted to standard format on the public user phonebook list.

    0 讨论(0)
  • 2020-12-11 01:48

    I would keep the original entered mess but would also insert a cleaned up form in the database. Which only kept the numbers less punctuation and spaces. Using the cleaned form would allow easy lookups without worrying about different possible entered styles.

    0 讨论(0)
  • 2020-12-11 01:50

    The main difficulty in canonicalizing phone numbers is determining the correct canonical format. Different countries have different ways of grouping numbers - and within a country, different numbers can be grouped differently.

    It used (once upon a decade or more ago) to be that case that in the UK, you had 01-234-2345, 021-234-1234, 0334-234234, even 092324-213; things are different in the UK now - generally more digits, and I'm not sure about the groupings any more (absence makes one's knowledge less current).

    Dealing with country prefixes and indicating the internal country dialling prefix is fun: +44 (0)1394-726629 is a UK number, country code 44; dialling from outside the UK, drop the 0; dialling inside the UK, do not include the international prefix but do include the 0. Do note that the form with (0) in it is in fact not valid if you follow the E.123 standard.

    This is similar to the problem of canonicalizing mail addresses - not as complex, but still bad.

    Also, as noted in my comment to HeavyWave's answer, forcing people to enter the phone number as a digit string with no punctuation is nasty. It's fine to store it that way; just present the data in a human readable format. There's far too much lazy web form programming out there.

    0 讨论(0)
  • 2020-12-11 01:51

    I usually like to store the number stripped and then format for display. Since I don't usually build application for use worldwide, I don't generally have to worry about the format. But in the case of an application for use all around the world, I would probably build a formatting module that formats according to the phone number's locale.

    0 讨论(0)
提交回复
热议问题