catamphetamine / libphonenumber-js

A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript

Home Page:https://catamphetamine.gitlab.io/libphonenumber-js/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ParsePhoneNumber mistakes the two first digits as the country code

sebsone opened this issue · comments

I have a react app with an input field for users to write their phone number which is saved as a state, then parsed, then displayed back into the input field. This means that the string is parsed each time a new digit is typed into the input field.

Most of the telephone numbers are Norwegian, which has a country code of 47. The issue is occurs when the phone number (with no prefix) starts with 470.

After typing in the 7th digit, the parsePhoneNumber method suddenly thinks the 47 is the country code rather than the number itself.

Example

Imagine that as the user types in their phone number this is whats being parsed:

console.log(parsePhoneNumber('470456', 'NO')); //6 digits
console.log(parsePhoneNumber('4704567', 'NO')); //7 digits
console.log(parsePhoneNumber('47045678', 'NO')); //8 digits

The output is:
image
Notice the nationalNumber on the second row.

This is especially a problem when using the state in the input field as the two first digits og the number will dissapear as the user is typing.

So is this a bug, or is there a way to work around it?

sorry this should probably be issued to google's library