Welcome, Guest. Please login or register.
Did you miss your activation email?

Author Topic: Format Phone Number: Javascript Function  (Read 2612 times)

ajtorre

  • Newbie
  • *
  • Posts: 47
  • You're given a spark of maddness. Dont lose it.
    • AOL Instant Messenger - jtscorpeo
    • View Profile
    • F. W. Davison & Company, Inc.
    • Email
Format Phone Number: Javascript Function
« on: October 29, 2010, 01:58:05 pm »
Recently, I had a CalcCol that had multiple telephone numbers in them with several different types of delimiters and formats.

I enlisted the aid of my colleague Ben to help me create a function that would re-format them if they were the standard set of 10 numbers. I plan to add to this to function to handle international numbers as well, but for now, its pretty good. It will work with a number string that contains 10 numbers and various delimiters.

If a string of not 10 numbers and various delimiters are passed to it, it will return the string and the words 'invalid format'


Code: [Select]
////////////////////////////// Start of function /////////////////////////////////////////
//
//              My thanks to Ben for helping me with this!! ~ ajtorre
//
//////////////////////////////////////////////////////////////////////////////////////////
//
var  formatPhoneNumber = function(input){
if (input == null) {return null}
//
// Strip all non-numeric characters and check for '\'...
//
var plain = "";
for(var i = 0;i < input.length; i++){
   var c = input.substring(i, i+1);
   if(!isNaN(c) && c != " " || !isNaN(c) && c != "\\" )
      {
      plain += c;
      }
}
//
// Make sure we have 10 numeric digits
//
var tenNumeric = /^[0-9]{10}$/;
if(tenNumeric.test(plain))
   {
   return "("
   + plain.substring(0, 3)
   + ") "
   + plain.substring(3, 6)
   + "-"
   + plain.substring(6, 10);
    }
else
   {
   //        
   // Not a valid phone number. Abort formatting, return number and 'invalid format'
   //
   return "'" + input + "'" + " ~ invalid format";
   }
}
////////////////// End of function ///////////


This worked beautifully.

I have programmed a different version with a section to process extensions and a separate portion to tackle probable international numbers. Why? Well... I'm still asking myself that... but it was fun to do and didn't really take that long.

Phone Numbers with an extensions, I had to make a few assumptions here...
~ ALL non numeric characters are ignored, except X or x.
~ A full 10 digit number with area code is present.
~ Extension qualifier is a X or an x and MUST be at position 11 in the phone number sequence.
~ No matter how many numbers AFTER the X, only take the first five number, discard all the rest
~ An X in the string of numbers disqualifies this string from being an international number. ( I suppose that I could have also taken into account for the reverse where if there was an X in the string but in the wrong position and to check if it could be an international number but I figured that if there was an x in the string it was meant to denote that there is an extension and that such was overkill
~ Assume no "1-" prefix.

 
International Phone numbers, I had to make a few assumptions here too...
~ Since there is no 'standard' and mutually agreed upon format for international numbers, I assumed this format:
~~ A number string of 14 characters or more, anything else is considered invalid.
~~ Where # is a number and n represents all trailing numbers with no further delimiters.
     ## ## ###-###-###-n

ANYTHING outside of these rules will be considered invalid.


Here is what I came up with:
Code: [Select]
///////////// Start of function ///////////
var  formatPhoneNumber = function(input){
// Strip all non-numeric characters except 'x' or 'X'
if (input == null) {return null}
var ext = "";
var extCnt = 0;
var plain = "";
var extFlag = 0;
var interNationalPhoneCount = 0;
var interNationalPhoneNumber = "";
var nbrsProcesedBeforeExtFlag = "";
for(var i = 0;i < input.length; i++)
{
  var c = input.substring(i, i+1);
  if(!isNaN(c) && c != " ")
  {
    plain += c;
    interNationalPhoneCount += 1;
  }
  if (c.toLowerCase() == "x" && extFlag == "0")
  {
    extFlag = 1;
    nbrsProcesedBeforeExtFlag = interNationalPhoneCount;
  }
  if (extFlag && c.toLowerCase() != "x" && !isNaN(c) && extCnt<5)
  {
    ext +=c;
    extCnt +=1;
  }
}
//
// Oooohhhhh mama.... here we go...
//
if (input == "")
  {
  return "'null' ~ invalid format."
  } else {
  switch (extFlag)
    {
    case 0:
       var tenNumeric = /^[0-9]{10}$/;
       if (interNationalPhoneCount >= 14)
       {
         interNationalPhoneNumber = plain.substring(0, 2)
         + " "
         + plain.substring(2, 4)
         + " "
         + plain.substring(6, 9)
         + "-"
         + plain.substring(9, 12)
         + "-";
         if (interNationalPhoneCount > 15)
         {
           interNationalPhoneNumber += plain.substring(12, 15) + "-" + plain.substring(15, interNationalPhoneCount);
         }else{
           interNationalPhoneNumber += plain.substring(12, interNationalPhoneCount);
         }
         return interNationalPhoneNumber
       }
       else if(tenNumeric.test(plain))
       {
         return "("
         + plain.substring(0, 3)
         + ") "
         + plain.substring(3, 6)
         + "-"
         + plain.substring(6, 10);
       }
       else
       {
//
// Not a valid phone number. Abort formatting
//
         return "'" + input + "'" + " ~ Invalid format!!";
       }
       break;
    case 1:
       if (nbrsProcesedBeforeExtFlag == 10)
       {
         return "("
         + plain.substring(0, 3)
         + ") "
         + plain.substring(3, 6)
         + "-"
         + plain.substring(6, 10)
         + " X" + ext;
       }
       else
       {
//  
// Not a valid phone number. Abort formatting
//
        return "'" + input + "'" + " ~ invalid Format!!";
        }
        break;
     default:
//
// Not a valid phone number. Abort formatting
//
        return "'" + input + "'" + " ~ invalid format!!";
      }
    }
  }
//////////////////// End of function ///////////



If any one has any improvements that they wish to comment about - please do so. I would be very interested to see them!
« Last Edit: November 19, 2010, 10:14:53 am by ajtorre »
Thanks and best regards,

Joseph Torre
Programmer/Analyst
F.W. Davison & Company, Inc.
Plymouth, MA

Informer v4.2.x, 4.3.x

The path to enlightenment is not always paved with the best of intentions; Road hazard signs are rarely posted...