Solved

How to display formatted phone numbers

Posted on 2004-08-22
2
189 Views
Last Modified: 2010-04-01
Hi,

I retrieve an all-numeric phone number from a database.  I then want to display a masked or formatted phone number on a web page.  I am using JSP to develop the web pages.  Can you tell me how I can do this?

For example:

I retrieve 2135551212 from the database and I want to display 213-555-1212
I retrieve 12135551212 from the database and I want to display 1-213-555-1212
I retrieve +4412345678 from the database and I want to display +4412345678

Thank you,
Dave
0
Comment
Question by:david_m_jacobson
2 Comments
 
LVL 27

Accepted Solution

by:
rrz earned 125 total points
ID: 11864482
You could use something like this.    
StringBuffer mask = new StringBuffer();
String  fromDb = unformatted  string retrieved from your database
if(fromDb.startsWith("+"){mask = fromDb;}
   else{ if(fromDb.startsWith("1"){
                     mask.append("1");
                     mask.append("-");
                     mask.append(fromDb.substring(1,4));
                     mask.append("-");
                     mask.append(fromDb.substring(4,7));
                     mask.append("-");
                     mask.append(fromDb.substring(7));
            }else{
                     mask.append(fromDb.substring(0,3));
                     mask.append("-");
                     mask.append(fromDb.substring(3,6));
                     mask.append("-");
                     mask.append(fromDb.substring(6));
                    }
0
 

Author Comment

by:david_m_jacobson
ID: 11864496
I wrote the following method to deal with this:

      public String formatPhoneNumber(String p_sPhoneNumber, int p_iCountryId) {
            String sAreaCode = "";
            String sPrefix = "";
            String sRemaining = "";
            
            //if the subscriber is not from the US or Canada then do not do anything
            if (p_iCountryId != Constant.US_COUNTRY && p_iCountryId != Constant.CAN_COUNTRY) return p_sPhoneNumber ;

            if (p_sPhoneNumber.length() == 11 && p_sPhoneNumber.substring(0,1).equalsIgnoreCase("1"))
                  return p_sPhoneNumber.substring(0,1) + "-" + p_sPhoneNumber.substring(1,4) + "-" + p_sPhoneNumber.substring(4,7) + "-" + p_sPhoneNumber.substring(7,11) ;
      
            if (p_sPhoneNumber.length() == 10){
                  sAreaCode = p_sPhoneNumber.substring(0,3);
                  sPrefix = p_sPhoneNumber.substring(3,6) ;
                  sRemaining = p_sPhoneNumber.substring(6,10);
                  
                  return sAreaCode + "-" + sPrefix + "-" + sRemaining ;
            }

            if (p_sPhoneNumber.length() == 7) {
                  return p_sPhoneNumber.substring(0,3) + "-" + p_sPhoneNumber.substring(3,7) ;       
            }

            return p_sPhoneNumber ;
      }
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
form submissions on a web page 2 142
servlet concurrency 13 84
love6 challenge java 31 128
memory representation of int[][][] a=new int[2][][]; 10 56
Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now