DEV Community

Cover image for phoneFormat & makeTelLink ColdFusion UDFs
James Moberg
James Moberg

Posted on

phoneFormat & makeTelLink ColdFusion UDFs

Standardization & validation is important, but good luck getting all you clients and your client's visitors on board with adhering to ISO standards. When it comes to phone numbers, we've embraced the E.164 international standard (ITU-T recommendation) in order to simplify integration with third-party SMS services (like Twilio), but that doesn't mean our clients like to see phone numbers in that format. When it comes to cosmetically formatting phone numbers, I personally prefer the (223) 456-7890 format, but some design-orientated clients demand that we use 123.456.7890 which I think more closely resembles an IP address.

In order to make our lives easier and quickly adapt when our clients change their minds (which tends to happen more than you'd think), we use dynamic server-side formatting for phone number data.

Apparently there are a lot of different ways to format phone numbers. The Gregg Reference Manual offers many possibilities (as referenced in this article.)

SalesForce has special logic formatting for the phone field to provide automatic formatting. I also found a blog with a decent article entitled "Why You Should Care About Phone Number Formatting In Your CRM (and How to Fix Them)" with some examples of phone number variations.

  • 2124567890
  • 212-456-7890
  • (212)456-7890
  • (212)-456-7890
  • 212.456.7890
  • 212 456 7890
  • +12124567890
  • +12124567890
  • +1 212.456.7890
  • +212-456-7890
  • 1-212-456-7890

I've been using a CFLib phoneFormat library to reformat US phone numbers and had made some tweaks to it over the years. To provide even better value for mobile users, we use a CFML script to attempt to detect mobile browsers and then auto-generate clickable TEL links on webpages using a makeTelLink UDF. (For more info regarding TEL, check out MSDN or IETF.)

In cases where the phone number may not match a US pattern, the phoneFormat string is returned unchanged while makeTelLink returns a non-linked string.

UPDATE 2024-11-15: Added optional support in both UDFs for phone extensions (off by default).

Enjoy!

Source Code for phoneFormat & makeTelLink

https://gist.github.com/JamoCA/4342da7f2388a3a0b38fb6b55b8c9c35

Top comments (0)