International number formatting on the web with JavaScript

When dialled from abroad, the leading 0 is replaced with +44 so the 07700 number in the above example would become +44 7700 900 123

Unfortunately, there isn't really an acceptable way of writing this for both audiences.

Numbers for an international audience

Most commonly (and the one form I generally accept) is placing the 0 within a bracket e.g. +44 (0)20 7946 0123 – however even this is met with some objections as brackets were traditionally used for the STD code (i.e. the above number could be written as (020) 7946 0123 indicating to people within the 020 area that they can omit that code)

I've also seen +44 020 7946 0123 which is just entirely wrong, (+44) 020 7946 0123, +44 (020) 7946 0123 and various other combinations which just cause confusion.

Another approach

The ideal answer is simply to show the visitor to your website the number they are most likely to dial

This can be achieved really easily with some simple JavaScript that will work in most cases. Simply wrap the number in a <span> element e.g.;

What this code does is simply looks at the browser timezone – chances are if it's not BST or GMT then they're not in the UK – and overwrites all occurrences of the .telnum class you've defined earlier with the internationally formatted number.

GeoIP?

Of course, GeoIP would be far more accurate but this requires an API call and I'm a great fan of not complicating (or slowing down!) web requests.

Using the timezone is a bit of a hack, but it works well enough in 90% or more of cases I expect.