marmelroy / PhoneNumberKit

A Swift framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber.
MIT License
5.13k stars 817 forks source link

PhoneNumberTextField with Irish Number and withPrefix = false #657

Closed jdconley closed 1 year ago

jdconley commented 1 year ago

New Issue Checklist

Steps to reproduce

Configure a PhoneNumberTextField like so:

        phoneNumberTextField.withDefaultPickerUI = true
        phoneNumberTextField.withPrefix = false
        phoneNumberTextField.modalPresentationStyle = .pageSheet
        phoneNumberTextField.withFlag = true
        phoneNumberTextField.withExamplePlaceholder = true
        phoneNumberTextField.textAlignment = .left
        phoneNumberTextField.textContentType = .telephoneNumber

Choose "Ireland" with the flag chooser.

Try to follow the mask format and enter a valid Irish number with leading "0" (or the example exactly "0850123456").

Expected result

I can enter the phone number in the national format and have it validated.

Actual result

The text field does not preserve the leading "0", which is the default format for the country. The Javascript version seems to pick up the 0 properly in the partial formatter.

I was stepping through textField(_:shouldChangeCharactersIn:replacementString:) and rawNumberString is "0" as expected but self.partialFormatter.formatPartial returns "" for the formatted national number.

It does work as expected with withPrefix = true but we were trying to just stick with the flag for country code.

Environment

SPM, SwiftUI project so there's a small wrapper around the text field. Tried with latest as well as 3.4.x.

bguidolim commented 1 year ago

Hey @jdconley

After some investigation, this is the expected behavior. To allow the leading zero, you must enable the flag withPrefix. For the PartialFormatter, the prefixes are +<country code> and leading zero if the region contains it. With the region selected correctly, the user won't need to add +<country code>, and enter the national number directly.

However, I understand that the default example placeholder can lead to misunderstandings depending on the expectations, but you're free to customize the placeholder by yourself and show a national number as placeholder.

jdconley commented 1 year ago

After some investigation, this is the expected behavior.

Thanks, I thought that might be the case. We have some confused users in countries where they typically include one or more leading zeros on their numbers. Filing support tickets. 🙂 We will have to use another approach.

MostafaBelihi commented 3 months ago

Here in Egypt we have the same case. Our country code is "+20" and national numbers also start with "0" like "01010012097". When combined, it becomes "+20 1010012097", so the first "0" is removed. I use the same as the original issue states, I removed the country code, which prevents the initial "0" in the national number. Many users here expect to enter there numbers with that "0". So, I think it should be allowed.