catamphetamine / libphonenumber-js

A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript
https://catamphetamine.gitlab.io/libphonenumber-js/
MIT License
2.77k stars 218 forks source link

Default country US returns other NANP numbers as invalid #368

Closed joallard closed 4 years ago

joallard commented 4 years ago

Entering a NANP country (US) as a default country, and entering a NANP number from another country, the library will return the number as invalid.

On the demo, section As you type:

  1. Enter 'US' as default country
  2. Enter '514 555-1212' as number

=> valid: false

I know the valid field is limited, but that seems kind of a stretch. Besides, "default country" doesn't mean "exactly this country".

catamphetamine commented 4 years ago

valid means “valid for the country specified”.

On Fri, 24 Jan 2020 at 08:05, Jonathan Allard notifications@github.com wrote:

Entering a NANP country (US) as a default country, and entering a NANP number from another country, the library will return the number as invalid.

On the demo https://catamphetamine.github.io/libphonenumber-js/, section As you type:

  1. Enter 'US' as default country
  2. Enter '514 555-1212' as number

=> valid: false

I know the valid field is limited, but that seems kind of a stretch. Besides, "default country" doesn't mean "exactly this country".

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/catamphetamine/libphonenumber-js/issues/368?email_source=notifications&email_token=AADUP3374P6WWM2APJCCEPLQ7JZITA5CNFSM4KLBCL6KYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IINWWTQ, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADUP33R6IM7KH4EOAGHODLQ7JZITANCNFSM4KLBCL6A .

catamphetamine commented 4 years ago

If you want NANPA validation then pass international numbers instead, by prepending +1 in case of NANPA.

joallard commented 4 years ago

Thank you for your response. The UX issue has since stopped existing, as users realized they were wrong. I will add a tooltip "Dear human, please take a few seconds of your existence to change the country in the dropdown menu or add "+1" even though the format is the same, just to satisfy this computer library code"

catamphetamine commented 4 years ago

I see, so you've fixed it. Just to reiterate: validation and phone number input are completely different tasks and shouldn't be mixed and should be performed at different stages.

catamphetamine commented 4 years ago

For example, inputting a phone number is AsYouType('US'), and validating it for any country afterwards is parsePhoneNumberFromString(asYouType.getNumber().number).isValid().