daddyz/phonelib

[Question] Allow only phone numbers with international prefix

Opened this issue · 2 comments

I have a Rails application that allows users to enter their phone number. Users are from different countries and I don't know the country for each user.

Is it possible to force users to enter only full phone numbers that include the country code?

# example
validates :phone_number, phone: true, require_international_prefix: true

This is especially useful for Whatsapp which requires "a full phone number in international format".

I made some experiments... At first I thought that using the normal validation did the job and refused numbers without an international prefix. Then I noticed that if the national number starts with something that seems an international prefix, then the number is accepted.

Example:

  • +41 442511234: valid and gives correct result
  • 442511234: reported as valid by this library, as if it was a GB number, while it is actually a CH number without international prefix!

Second example seems a bug to me... doesn't need to have a + or 00 in front of it in order to be a number with international prefix? In my opinion it should be considered invalid (i.e. country_code should be considered nil in the second example)

In latest version "442511234" is not valid, but I will keep this as enhancement to expect for e164 only in validator for certain params. Thanks for reporting