Reputation: 326
I'm trying to get international phone numbers to format properly but it doesn't seem to be adding the spaces within the number, only for the country code.
let phoneNumber = parsePhoneNumber('+3802342342345', 'UA');
return phoneNumber.formatInternational();
So the result I would expect to see is:
+380 234 234 2345
but instead I get
+380 2342342345
without the spaces in the actual phone number.
Upvotes: 0
Views: 2653
Reputation: 10247
Seems like your number is invalid. Some of the number formats I found here
let phoneNumber = libphonenumber.parsePhoneNumber('+3802342342345', 'UA').formatInternational();;
console.log(phoneNumber)
console.log(libphonenumber.isValidNumber(phoneNumber))
//+380 44 xxx-xx-xx (international call to Kyiv)
let a = libphonenumber.parsePhoneNumber('+380444234245', 'UA').formatInternational();
console.log(a)
console.log(libphonenumber.isValidNumber(a))
//+380 45 94x-xx-xx (international call to Brovary)
let b = libphonenumber.parsePhoneNumber('+380459434245', 'UA').formatInternational();
console.log(b)
console.log(libphonenumber.isValidNumber(b))
//+380 48 xxx-xx-xx (international call to Odessa)
let c = libphonenumber.parsePhoneNumber('+380489434245', 'UA').formatInternational();
console.log(c)
console.log(libphonenumber.isValidNumber(c))
//+380 48 2xx-xx-xx (international call to 6-digit numbers in Odessa)
let d = libphonenumber.parsePhoneNumber('+380482434245', 'UA').formatInternational();
console.log(d)
console.log(libphonenumber.isValidNumber(d))
<script src="https://cdnjs.cloudflare.com/ajax/libs/libphonenumber-js/1.9.48/libphonenumber-js.min.js"></script>
The library seems to take the default digits together
Upvotes: 2