Solved

How to display formatted phone numbers

Posted on 2004-08-22
2
193 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Export JSON response data to Excel in IE9 3 90
constructor overloading 2 94
HTML5 vs JSP - REST based application development 4 1,355
IE doesn't Invoke servlet in iframe 1 123
The world seems to conceive of a curious bubble separating IT from “the business.”  More so than just about any other pursuit in the commercial world, people think of IT as some kind of an island.
Read the original post on Monitis Blog. Believe it or not, the most important thing about the website of your business is not what’s on it but how fast it loads. Yes, that’s right!    As you can see on this infographic (an oldie but goodie!), …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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