PhoneFormatter.cfc 1.0
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.