Take the 2-minute tour ×

I have an array of phone numbers and I need to find if a particular phone number is in it. What I tried doing at first was if(arr.indexOf(phoneNumber) != -1) { bla.. }. And it worked - sometimes.
I later discovered that since the number/s would arrive from different phones/entry forms, some people use country codes (like +1-xxx-xxx-xxxx), some wouldn't. Some use spaces as seperators and some just put in 10 digits in a row. In short - hell to compare.

What I need is an elegant solution that would allow me to compare, hopefully without having to replicate or change the original array.

In C++ you can define comparison operators. I envision my solution as something like this pseudo-code, hopefully using some smart regex:

function phoneNumberCompare(a, b) {
    a = removeAllSeperators(a); //regex??
    a = a.substring(a.length, a.length - 10);
    b = removeAllSeperators(b); //regex??
    b = b.substring(b.length, b.length - 10);
    return (a < b ? -1 : (a == b ? 0 : 1));  //comaprison in C++ returns -1, 0, 1

and use it like if(arr.indexOf(phoneNumber, phoneNumberCompare) != -1)

Now, I know a solution like this construct does not exist in JavaScript, but can someone suggest something short and elegant that achieves the desired result?

As always, thanks for your time.

PS: I know indexOf() already has a second parameter (position), the above is just ment to illustrate what I need.

share|improve this question
I would suggest looking into Google's phone number handling library: code.google.com/p/libphonenumber –  James Allardice Aug 12 '11 at 7:55
Thanks James. Looks like a cool library, but maybe a tad to big for my small problem :) –  Traveling Tech Guy Aug 12 '11 at 8:05
the above exemple going to run a loop, and running the function removeAllSeperators 2 times per row, probely going faster if you write the loop yourself, then use a function that makes the loop, whats so bad whit loops? –  Puggan Se Aug 12 '11 at 8:06
add comment

2 Answers

up vote 0 down vote accepted

You really should sanitize all the data, both at collection and in the DB.

But for now, here's what you asked for:

function bPhoneNumberInArray (targetNum, numArray) {
  var targSanitized   = targetNum.replace (/[^\d]/g, "")
                                 .replace (/^.*(\d{10})$/, "$1");
  //--- Choose a character that is unlikely to ever be in a valid entry.
  var arraySanitized  = numArray.join ('Á').replace (/[^\dÁ]/g, "") + 'Á';

  //--- Only matches numbers that END with the target 10 digits.
  return (new RegExp (targSanitized + 'Á') ).test (arraySanitized);

How it works:

  1. The first statement removes everything but digits (0-9) from the target number and then strips out anything before the last 10 digits.
  2. Then we convert the array to be searched into a string (very fast operation).

    1. When joining the array, we use some character to separate each entry.
    2. It must be a character that we are reasonably sure would never appear in the array. In this case we chose Á. It could be anything that doesn't ever appear in the array.
    3. So, an array: [11, 22, 33] becomes a string: 11Á22Á33Á, for example.
  3. The final regex, then searches for the target number immediately followed by our marker-character -- which signals the end of each entry. This ensures that only the last 10 digits of an array's number are checked against the 10-digit target.


var numArray = ['0132456789', "+14568794324", "123-456-7890"];

bPhoneNumberInArray ("+1-456-879-4324", numArray)      // true
bPhoneNumberInArray ("+14568794324", numArray)         // true
bPhoneNumberInArray ("4568794324", numArray)           // true
bPhoneNumberInArray ("+145 XXX !! 68794324", numArray) // true !
bPhoneNumberInArray ("+1-666-879-4324", numArray)      // false
share|improve this answer
Hi Brock. Thanks! I'll use your code, if you could just explain what the 'Á' chaacter is :) –  Traveling Tech Guy Aug 12 '11 at 17:14
The 'Á' character is just an arbitrary, "safe" separator character. I've updated the answer to try to explain it better. –  Brock Adams Aug 12 '11 at 22:29
It works only with US phone number format in which country code has only 1 digit (1), not work with country code has 2 or 3 digits –  Bao Le Oct 10 '12 at 1:17
@BaoLe, As stated, this is the wrong approach but it's "Quick and Dirty" like the OP wanted. The correct thing to do is to (1) sanitize and organize the data, (2) use a library that handles all of the I8N vagaries. EG: Google's phone number handling library ... ... for this Q&D answer, changing the 2nd replace to .replace (/^.*(\d{10,12})$/, "$1"); might work (untested). –  Brock Adams Oct 10 '12 at 2:02
add comment

You should sanitize both the input and all array values, to make sure they conform to the same ruleset.

Just create a function called sanitizePhonenumber, where you strip (or add, depending on your preferences) the country code and all other signs you dont want there.

After that you can just compare them as you are doing now.

share|improve this answer
I understand that. I'm just trying to see if there's a way I can avoid sanitizing the full array and the compared number. Or, an elegant way to do it without looping over the array. –  Traveling Tech Guy Aug 12 '11 at 7:59
add comment

Your Answer


