0

Are there best practices for validating, recording AND formatting phone numbers?

Thanks for the link to AN answer regarding AN ELEMENT of my question: a regex for validating. But please recognize that it does not answer the question that was asked. There are no doubt countless suggestions for validating, recording and formatting phone numbers; many of which are highly rated. However, since this is a common area that a multitude of developers have wrestled with for a long period of time, I asked if there are generally accepted best practices for the whole package.

Please read and respond to the stated question:

Are there best practices for validating, recording and formatting phone numbers?

  • Starting with the data element, are phone numbers better stored as integers or strings (or something else)?
  • Next, should delimiters be stored with the numbers or stripped out?
    (In other words, should (212) 555-1212 be recorded as 2125551212 and then formatted appropriately in a view?)
  • Next, what's the best way to deal with formatting alternatives in different countries? Perhaps if the country code was recorded in a separate field, a SWITCH statement could be used to select the right formatting for the view (or validation) of the phone number itself.
  • Finally, are there good examples of regular expressions or other methods for validating phone number inputs?

I recognize the this is really a collection of questions, each of which likely has opinions scattered all over SO and the web. Since validating, recording and displaying formatted phone numbers is such a common task, it seems like there should be some standard methodologies or best practices; and my question is, "Are there generally accepted practices, and where can I find them?"

jcropp
  • 1,236
  • 2
  • 10
  • 29
  • I'd store them as string and be done with it. Too many possible ways of inserting it from the user (or you use some js input mask), too many different formats (unless you only deal with national phone numbers), sometimes can't be integers in the first place (think of international prefix: 0039 or +39 for example) – Damien Pirsy Jun 22 '14 at 06:13
  • I prefer to strip all nondigits and store them as int. It also simplifies validation. You can format on display. – mleko Jun 22 '14 at 06:39
  • @mieko At least here, many numbers start with a zero (city prefix). Mobile numbers start with 3. If you strip the leading zero, say, to get an int you can't tell from a 0342 (city prefix) to a 342 (mobile prefix) All without accounting for the 0039 international prefix. It can get messy easy, or you could store it a string and job done. – Damien Pirsy Jun 22 '14 at 08:58

0 Answers0