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

前端 未结 12 1511
鱼传尺愫
鱼传尺愫 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 02:01

    What's your userbase?

    If they're going to be limited geographically (i.e., US-only) and you're going to validate numbers strictly, then format the number canonically for them -- i.e., strip out any formatting they used (like periods between numbers...) and put in the dashes (do not fail validation if they don't stick to your formatting... that's just mean). I'd store that cleaned-up version in the DB as well, not a stripped number; it makes your life a bit easier when generating custom reports, etc..

    If you might have users/numbers from all over the world, it might be better to save the formatting they used. Also don't forget the case that sometimes US residents are currently traveling and using a foreign number: don't block them unintentionally.

    Either way: make sure you DON'T define the column as numeric, or make it too small. International numbers with formatting can easily be over 16 chars long.

提交回复
热议问题