The Largest Repository of ColdFusion Knowledge in The World for More Than 12 Years

ColdFusion on Ulitzer

Subscribe to ColdFusion on Ulitzer: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get ColdFusion on Ulitzer: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


CFDJ Authors: AppDynamics Blog, Michael Kopp, Tad Anderson, Bob Gourley, Jayaram Krishnaswamy

Related Topics: ColdFusion on Ulitzer

CFDJ: Article

PhoneFormatter.cfc 1.0 in ColdFusion

I typically store phone numbers in one field instead of using a separate field for each part of a phone number

Steve Bryant's Blog

I typically store phone numbers in one field (instead of using a separate field for each part of a phone number). While this usually works quite well, I have discovered that it can cause issues when I want a phone number to display in the same format regardless of how it was originally entered.

This is relatively easy to do if you handle it as the data comes in, but if you don't (either as oversight or because the data comes from outside the system) then it is a more difficult task.

I created PhoneFormatter to handle this task. Actually, it can handle formatting the data on an individual string or on a field across a whole table.

PhoneFormatter does require DataMgr. It also requires you to pass in the format you want to use for the phone number (using zeros as place-holders for digits). You should also pass in a default area code for numbers that don't have one. So the arguments for the init() method are:

  • DataMgr: An instantiated DataMgr component
  • Format: The desired format for phone number (using zeros as place holders for digits)
  • DefaultAreaCode: (optional) The area code you want PhoneFormatter to assign to phone numbers that don't have one.

To format a single phone number, pass it to the fixPhoneNumber() function (optionally passing in an area code as a separate argument).

To format every row for one field in a table, use the fixPhoneNumbers() method.

  • table: The name of the table in which you have the phone number.
  • idfield: The primary key field for the table.
  • phonefield: The field in the table in which you have the phone number.

PhoneFormatter may not solve a problem that you have, but if it does it can certainly be useful.

It is open source and free for any use. Feel free to download PhoneFormatter and try it out. 

More Stories By Steve Bryant

Steve Bryant is the founder and CEO of Bryant Web Consulting LLC (www.bryantwebconsulting.com) and teaches ColdFusion at Breakaway Interactive (www.breakawayinteractive.com). He got his BA in philosophy at Oklahoma State University. Steve, one of the top ColdFusion developers in the country, still has no idea how that led to a career in Web development. Steve blogs at steve.coldfusionjournal.com as one of CFDJ's published bloggers.

Comments (1)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.