Solved

How to display formatted phone numbers

Posted on 2004-08-22
2
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft is moving in-place eDiscovery & hold from ECP to EOP console under Content Search in Search and Investigation Options.  In this post, I will be showing you how to export emails to a PST file using the Content Search Options.
Article by: Justin
In light of the WannaCry ransomware attack that affected millions of Windows machines, you might wonder if your Mac needs protecting. Yes, it does and here is how to do it.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

688 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