format text field to phone number (###) ###-####

Is there an inherent way for the text field to be able to automatically show the phone number using a format of (###) ###-####, or is that something I have to code myself?

Answered by Claude31 in 266391022

You'll have to do it yourself.


But you'll find many examples, such as :

h ttps://stackoverflow.com/questions/32364055/formattting-phone-number-in-swift


Take care of the input number. 10 or 11 digits ?

Format may be:

(###) ###-#### : OK for US

or #-(###) ###-#### : if you want the internation code

or ## (#) # ####-#### : in countries as France even presented as 33 (0) 6 07 123456 where (0) is only for local call

If international, country code may be 1 digit (US), 2 digits (eg France 33) or 3 digits in many countries (Africa, Asia, …)

See for reference the telephone numbering plan :

h ttps://en.wikipedia.org/wiki/Telephone_numbering_plan

and

h ttps://en.wikipedia.org/wiki/List_of_country_calling_codes


It is pretty complex formatting to cover all the cases.

Accepted Answer

You'll have to do it yourself.


But you'll find many examples, such as :

h ttps://stackoverflow.com/questions/32364055/formattting-phone-number-in-swift


Take care of the input number. 10 or 11 digits ?

Format may be:

(###) ###-#### : OK for US

or #-(###) ###-#### : if you want the internation code

or ## (#) # ####-#### : in countries as France even presented as 33 (0) 6 07 123456 where (0) is only for local call

If international, country code may be 1 digit (US), 2 digits (eg France 33) or 3 digits in many countries (Africa, Asia, …)

See for reference the telephone numbering plan :

h ttps://en.wikipedia.org/wiki/Telephone_numbering_plan

and

h ttps://en.wikipedia.org/wiki/List_of_country_calling_codes


It is pretty complex formatting to cover all the cases.

You can actually do this in 1 line:

iPhoneNumberField("", text: $text)

See more: https://github.com/MojtabaHs/iPhoneNumberField
format text field to phone number (###) ###-####
 
 
Q