List of Topics

SfC Home > Web Design > SQL > ColdFusion >

Changing Data to Telephone Format in ColdFusion

by Ron Kurtus (updated 27 May 2023)

Sometimes telephone numbers are stored in a database as a text string of digits, such as 4145551234. It can be difficult to display those numbers in the standard telephone format. Also, data-entry can be confusing, with people entering in different formats.

A preferred format would include dashes, such as 414-555-1234. A method to include the dashes is to first create a new row in your database table and then use the INSERT function to include the dashes. Test this by adding new telephone numbers.

Questions you may have include:

This lesson will answer those questions.



Create new row

It is a good idea to create a new telephone number row or field, in which to work, just in case you screw up. Let's name it TelephoneNo2.

Make a simple ColdFusion page to copy the numbers from one row to another. Again, so you don't accidentally delete your list, do a test case for a single ID.

<CFQUERY DATASOURCE="xxx">
UPDATE TableName
SET TelephoneNo2 = TelephoneNo
WHERE ID = 25
</CFQUERY>

If it works correctly, get rid of WHERE ID = 25 and run the UPDATE query again, populating the new row.

Convert strings

The method to convert 4145551234 into 414-555-1234 is to use the INSERT function in a query.

INSERT("-", "4145551234", 3) will insert "-" 3 spaces from the front of the 4145551234 to give you 414-5551234. You can then next the INSERT function to add the second dash.

You query for the Telephone values, then update them with INSERT to add the dashes. You have the backup TelephoneNo2 in case things don't work as planned.

Query for values

<CFQUERY NAME="tel" DATASOURCE="xxx">
SELECT ID, TelephoneNo
FROM TableName
WHERE TelephoneNo <> ' '
</CFQUERY>

 Checking TelephoneNo for not equal to ' ' (empty) is done to avoid errors for records with no telephone number. Instead of ' ', you may have to use NULL, depending on how your database is set up. Typcially a MS SQL database requires the use of NULL.

Loop and update

Next, you loop through each item in the query and update the TelephoneNo row. Note how the INSERT function is nested to add the two dashes in the correct position. You also must include ID = #ID#, otherwise every new value will be updated to the last one in the column.

<CFLOOP QUERY="tel">

<CFQUERY DATASOURCE="xxx">
UPDATE TableName
SET TelephoneNo = '#INSERT("-", INSERT("-", TelephoneNo, 3), 7)#'
WHERE TelephoneNo <> ' '
AND ID = #ID#
</CFQUERY>

</CFLOOP>

Delete backup

If everything worked fine, you can delete your backup TelephoneNo2 row . 

Adding telephone numbers

To make sure new data is added to the database in the correct telephone format, you should use the following CFINPUT statement:

<CFINPUT TYPE="text" NAME="TelephoneNo"  VALIDATE="telephone" MESSAGE="You must enter the phone number in the xxx-xxx-xxxx format" SIZE="12">

If the data is not entered in the proper format, a warning message will appear. 

Other methods

I am sure there are other ways to perform this task or to solve this problem. This one seems to work pretty good, with no too much pain.

Summary

To convert a text string of digits to the telephone format, you can use a nested INSERT function and add the required dashes. It is a good idea to make a backup row before making your changes, in case things don't work as planned. To  input data, use VALIDATE="telephone" in CFINPUT.


Be honorable in your work


Resources and references

Ron Kurtus' Credentials

Websites

ColdFusion Resources

Books

(Notice: The School for Champions may earn commissions from book purchases)

Top-rated books on ColdFusion


Students and researchers

The Web address of this page is:
www.school-for-champions.com/coldfusion/
telephone.htm

Please include it as a link on your website or as a reference in your report, document, or thesis.

Copyright © Restrictions


Where are you now?

School for Champions

ColdFusion topics

Changing Data to Telephone Format in ColdFusion




ColdFusion topics

Basics

Development methods

Specific applications

Also see



Let's make the world a better place

Be the best that you can be.

Use your knowledge and skills to help others succeed.

Don't be wasteful; protect our environment.

You CAN influence the world.





Live Your Life as a Champion:

Take care of your health

Seek knowledge and gain skills

Do excellent work

Be valuable to others

Have utmost character

Be a Champion!



The School for Champions helps you become the type of person who can be called a Champion.