catamphetamine / react-phone-number-input

React component for international phone number input
http://catamphetamine.gitlab.io/react-phone-number-input/
MIT License
914 stars 193 forks source link

Is there a utility to convert local to E.164 number? #379

Closed andregoldstein closed 3 years ago

andregoldstein commented 3 years ago

Hi there, I have a use case for going through my existing phone numbers and trying to convert them to E.164 numbers to store in the database.

I don't think parsePhoneNumber works unless the number is already in that format?

I guess the library does something library using the provided input. Is there something usable in the library or is it better to go through and slice the numbers together.

Example would be converting local French numbers to E.164. I guess in this case I could manually remove the 0 and add on the +33. Thanks again for the library!

0612345678 to +33612345678

If there isn't something in-built I guess if I ensure that it's 100% a ten digit number starting with 04, 05 etc I can do:

number = number.slice(1, number.length)
number = "+33" + number
andregoldstein commented 3 years ago

Apologies, just realised parsePhoneNumber takes a country code as a second parameter.

parsePhoneNumber('0612345678', 'FR')

Works great. I guess I'll just make my own detection if it's FR/GB number

andregoldstein commented 3 years ago

I think what I might do is go through all my number starting with 00 and replace with +, then run them through the parsePhoneNumber function

catamphetamine commented 3 years ago

Apologies, just realised parsePhoneNumber takes a country code as a second parameter.

Yes, it's just a re-export of a libphonenumber-js function.

. I guess in this case I could manually remove the 0 and add on the +33.

I wouldn't advice that. Use libphonenumber-js's parsePhoneNumber() instead.

I think what I might do is go through all my number starting with 00 and replace with +, then run them through the parsePhoneNumber function

Don't mess with phone numbers manually. Instead, use libphonenumber-js's parsePhoneNumber() function.

andregoldstein commented 3 years ago

Thanks @catamphetamine I think the problem I have is that 0044... doesn't convert properly (unless I'm mistaken?)

catamphetamine commented 3 years ago

I think the problem I have is that 0044... doesn't convert properly (unless I'm mistaken?)

Well, that depends on the country. You could check the number here: https://libphonenumber.appspot.com/

andregoldstein commented 3 years ago

Thanks @catamphetamine, I tried that but similarly, it didn't find valid numbers for GB or FR starting 0044 or 0033 instead of +44 or +33

I believe when describing international numbers a lot of people use the two terms interchangeably, it would be cool if typing

00447826278080 converted to +447826278080

https://libphonenumber.appspot.com/phonenumberparser?number=00447826278080 https://libphonenumber.appspot.com/phonenumberparser?number=%2B447826278080

catamphetamine commented 3 years ago

It doesn convert 00447826278080 to +447826278080, doesn't it? Google's demo: https://libphonenumber.appspot.com/phonenumberparser?number=00447826278080&country=GB

andregoldstein commented 3 years ago

Ah yes I see that it does if I provide the country code, thanks. My use case here though is to go over a batch of existing numbers with possibly different country codes