简体   繁体   中英

International phone number validation and display

I have phone numbers in a database that are submitted through a form to be listed on my site. There are many variations. Some are international, some from the US. Some people just put in the local version, others include the international format.

I am trying to make the numbers clickable, when possible, for mobile phones.

I am creating the page using php, so I can process the numbers before having to make display decisions.

Since I am starting with human input, I will use that for the display on the page and use an a tag with href="tel: prefix.

<a href="tel:+12223334444">(222) 333-4444</a>

My understanding is that all non-numeric characters should be stripped, with the possible exception of 'x', which could denote an extension, and a leading '+' for international calls. Is there any reason not to use the leading '+' for any number? Or does it require that a country code is present? Is making sure that a US number starts with '1' all that is needed to make it dial-able internationally?

I have read that some people from the UK sometimes erroneously use '(0)' when they submit the number. Should this just be stripped out, or should the '0' remain, or should it be replaced with their country code.

Are leading zeros valid in some numbers?

The essence of what I want to achieve is this:

1) Determine if the number (minus any decoration, parenthesis, dashes, etc.) is international ready, or can be made international ready by adding a leading '+' or whatever else might be necessary. I can't seem to find a definitive check for a valid international number, but surely it exists.

2) If possible, make the number dial-able for mobile by using a link, without creating confusion or errors on desktop browsers. If the number is not dial-able(fails validation), or can not be made dial-able (using some basic repair), it will just be displayed as input by the user.

I assure you, that's pretty much a lot that you want, you'll not be coding it yourself.
libphonenumber is an excellent library for your wishes:
https://code.google.com/p/libphonenumber/

For any further info on it:
https://stackoverflow.com/questions/tagged/libphonenumber

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM