?
Solved

How to display formatted phone numbers

Posted on 2004-08-22
2
Medium Priority
?
197 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 28

Accepted Solution

by:
rrz earned 500 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
With more and more companies allowing their employees to work remotely, it begs the question: What are some of the security risks involved with remote employees and what actions should we take to secure them?
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 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