• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Phone Number Formatting
2
3## Use Cases
4
5The number of digits, combination mode, and display mode of phone numbers vary according to countries and regions. Besides, the dialing mode and number format may also differ under different situations. For example, to make a cross-area call in Chinese mainland, you usually need to type **0** and then dial the area code plus an 8-digit phone number. To make a call in Hong Kong or Macao, you need to dial the number in a different way.
6
7Through phone number formatting, your application will be able to present phone numbers to users in their expected format.
8
9## How to Develop
10
11Phone number formatting is implemented through the [format](../reference/apis-localization-kit/js-apis-i18n.md#format8) API of the [PhoneNumberFormat](../reference/apis-localization-kit/js-apis-i18n.md#phonenumberformat8) class. The development procedure is as follows:
12
131. Import the **i18n** module.
14   ```ts
15   import { i18n } from '@kit.LocalizationKit';
16   ```
17
182. Create a **PhoneNumberFormat** object.
19
20   You can set different phone number formats through **PhoneNumberFormatOptions**. For details, see Table 1.
21
22   ```ts
23   let phoneNumberFormat: i18n.PhoneNumberFormat = new i18n.PhoneNumberFormat(country: string, options?: PhoneNumberFormatOptions);
24   ```
25
263. Format a phone number.
27   ```ts
28   let formattedPhoneNumber: string = phoneNumberFormat.format(phoneNumber: string);
29   ```
30
314. Check whether the phone number is correct and obtain its home area.
32   ```ts
33   The let isValidNumber: boolean = phoneNumberFormat.isValidNumber(phoneNumber: string); // Check whether the phone number is correct.
34   let locationName: string = phoneNumberFormat.getLocationName(number: string, locale: string); // Obtain the home area of the phone number.
35   ```
36
37**Phone Number Formatting Options**
38
39The following uses the phone number **158\*\*\*\*2312** and the country code **CN** as an example to show the values of **PhoneNumberFormatOptions** and corresponding display effects.
40
41**Table 1** Phone number formatting type (type)
42
43| Value| Display Effect|
44| -------- | -------- |
45| E164 | +86 158\*\*\*\*2312 |
46| INTERNATIONAL | +86 158 \*\*\*\* 2312 |
47| NATIONAL | 158 \*\*\*\* 2312 |
48| RFC3966 | tel:+86-158-\*\*\*\*-2312 |
49| TYPING | 158 \*\*\* |
50
51
52**Development Example**
53
54```ts
55// Import the i18n module.
56import { i18n } from '@kit.LocalizationKit';
57
58// Format the phone number.
59let phoneNumberFormat: i18n.PhoneNumberFormat = new i18n.PhoneNumberFormat('CN');
60let formattedPhoneNumber: string = phoneNumberFormat.format('158****2312'); // formattedPhoneNumber = '158 **** 2312'
61
62// Set the format type of the phone number to RFC3966.
63let RFC3966Format: i18n.PhoneNumberFormat = new i18n.PhoneNumberFormat('CN', { type: 'RFC3966' });
64formattedPhoneNumber = RFC3966Format.format('158****2312'); // formattedPhoneNumber = 'tel:+86-158-****-2312'
65
66// Check whether the phone number is valid.
67let isValid: boolean = phoneNumberFormat.isValidNumber('158****2312'); // isValid = true
68
69// Display the home area of the phone number in the specified language.
70let locationName: string = phoneNumberFormat.getLocationName('158****2312', 'en-GB'); // locationName = 'XiAn, Shanxi'
71
72// Format the phone number being dialed.
73let typingFormat: i18n.PhoneNumberFormat = new i18n.PhoneNumberFormat('CN', { type: 'TYPING' });
74let phoneNumber: string = '0755453';
75let formatResult: string = ''; // Format the dialed number as follows: formatResult = '0755 453'
76for (let i = 0; i < phoneNumber.length; i++) {
77  formatResult += phoneNumber.charAt(i);
78  formatResult = typingFormat.format(formatResult);
79}
80```
81