?
Solved

How to display formatted phone numbers

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 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