views:

171

answers:

3

Hi Guys,

Is there a javascript library the can recognize phone numbers in a web page? Just like what skype did on their firefox plugin.

Or do you know a way on how to do it? Websites or any tutorial that do the same would be very helpful.

Your reply is greatly appreciated.

Best,

A: 

http://examplet.buss.hk/jquery/format.php

mike clagg
Perhaps you could explain in more detail how that page answers his question?
Mark Byers
Thank you guys for your reply. I'm considering using regular expression, that's pretty much the only way to recognize a phone number. My problem is how can I implement it on a page. During page load, I wanted to get all the phone numbers that is on that page.
samer
I don't have any input box, During page load I wanted to find all the phone numbers on to that page then convert it into a link.
samer
+1  A: 

To find matches within a string, you'll want to use a regular expression. This one (although somewhat lengthy) works pretty well:

^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$

(found here)

This will match "2405525009", "1(240) 652-5009", and "240/752-5009 ext.55", but not "(2405525009" or "2 (240) 652-5009".

To find all matches, you may want to repeatedly apply the exec() method in a while loop, as seen here.

Adam
Thank your for your reply. Yes, regular expression is pretty much the tool but I don't want any input box on my page. I just want to crawl to the page and recognize those phone numbers then convert it to link.
samer
+1  A: 

Someone else may have a better way of doing this, but this seems to give you a link around each phone number.

I just used my simple regular expression, so you may want to substitute the one that Adam provided.

$(document).ready(function () {

    $('*','body').each(function() {
        $(this).html( $(this).html().replace(/(\d\d\d-\d\d\d-\d\d\d\d)/g,'<a href="#">$1</a>') );
    });

});

Hope it helps.


EDIT:

It may work as well, or better, with this version.

$(document).ready(function () {
    $('body').html( $('body').html().replace(/(\d\d\d-\d\d\d-\d\d\d\d)/g,'<a href="#">$1</a>') );
});

I don't know if there are any pitfalls, but seems to work with a fairly simple page.

patrick dw