Welcome, Guest. Please login or register.

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

ajtorre

  • Newbie
  • *
  • Posts: 47
  • You're given a spark of maddness. Dont lose it.
    • View Profile
    • F. W. Davison & Company, Inc.
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]
////////////////////////////// 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 ///////////[/code]


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.

[b]Phone Numbers with an extensions[/b], I had to make a few assumptions here...
~ ALL non numeric characters are ignored, [i]except [b]X[/b] or [b]x[/b].[/i]
~ A full 10 digit number with area code is present.
~ Extension qualifier is a [b][i]X[/i][/b] or an [b][i]x[/i][/b] and MUST be at position 11 in the phone number sequence.
~ No matter how many numbers AFTER the [i][b]X[/b][/i], only take the first five number, discard all the rest
~ An [b][i]X[/i][/b] in the string of numbers disqualifies this string from being an international number. [sub]( I suppose that I could have also taken into account for the reverse where if there was an [b][i]X[/i][/b] 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[/sub]
~ Assume no "1-" prefix.


[b]International Phone numbers[/b], 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 [b][i]#[/i][/b] is a number and [b][i]n[/i][/b] represents all trailing numbers with [i]no further delimiters.[/i]
     [tt]## ## ###-###-###-n[/tt]

ANYTHING outside of these rules will be considered invalid.


Here is what I came up with:
[code]
///////////// 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 ///////////

[/code]


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,

[b][u]Joseph Torre[/u]
Programmer/Analyst
F.W. Davison & Company, Inc.
Plymouth, MA

Informer v4.2.x, 4.3.x[/b]
[i]The path to enlightenment is not always paved with the best of intentions; Road hazard signs are rarely posted...[/i]