# Phone

> Validate Pakistani mobile numbers, format them as E.164, and find the network a number was first issued on.

Source: https://pakdatakit.dev/docs/phone/

`import { validatePhone, formatPhone, analyzePhone } from 'pakdatakit/phone'` · `import { getNetwork } from 'pakdatakit/networks'`

Accepts the forms people actually type (`03…`, `+92…`, `0092…`, `+92 (0) 3…` and `3…`, with spaces, dashes, dots or Urdu digits) and checks that the `03XX` prefix belongs to a mobile operator.

## Examples

**TypeScript**

```ts
import { validatePhone, formatPhone, analyzePhone, getNetwork } from 'pakdatakit';

validatePhone('0300-1234567');
// → true

formatPhone('0300 1234567');
// → '+923001234567'

analyzePhone('+92 345 1234567');
// → {
//     "valid": true,
//     "formatted": "+923451234567",
//     "network": "Telenor"
//   }

getNetwork('03211234567');
// → 'Jazz'
```

**Python**

```python
from pakdatakit import validate_phone, format_phone, analyze_phone, get_network

validate_phone('0300-1234567')
# → True

format_phone('0300 1234567')
# → '+923001234567'

analyze_phone('+92 345 1234567')
# → {
#     "valid": True,
#     "formatted": "+923451234567",
#     "network": "Telenor"
#   }

get_network('03211234567')
# → 'Jazz'
```

**Dart**

```dart
import 'package:pakdatakit/pakdatakit.dart';

validatePhone('0300-1234567');
// → true

formatPhone('0300 1234567');
// → '+923001234567'

analyzePhone('+92 345 1234567');
// → {
//     "valid": true,
//     "formatted": "+923451234567",
//     "network": "Telenor"
//   }

getNetwork('03211234567');
// → NetworkName.jazz
```

**C#**

```csharp
using PakDataKit;

Phone.Validate("0300-1234567");
// → true

Phone.Format("0300 1234567");
// → "+923001234567"

Phone.Analyze("+92 345 1234567");
// → {
//     "valid": true,
//     "formatted": "+923451234567",
//     "network": "Telenor"
//   }

Networks.GetNetwork("03211234567");
// → "Jazz"
```

**PHP**

```php
use PakDataKit\Networks;
use PakDataKit\Phone;

Phone::validate('0300-1234567');
// → true

Phone::format('0300 1234567');
// → '+923001234567'

Phone::analyze('+92 345 1234567');
// → {
//     "valid": true,
//     "formatted": "+923451234567",
//     "network": "Telenor"
//   }

Networks::getNetwork('03211234567');
// → 'Jazz'
```

## Reference

### validatePhone()

```ts
function validatePhone(phone: string): boolean;
```

**Parameters.** `phone`: a mobile number in any of the forms above.

**Returns** `true` for a mobile number with an allocated `03XX` prefix. Landlines, UAN and toll-free numbers return `false` (see `analyzeNumber()` for those).

**Related:** [`formatPhone()`](#formatphone), [`analyzePhone()`](#analyzephone).

### formatPhone()

```ts
function formatPhone(phone: string): string | null;
```

**Returns** the number in E.164 form (`+923001234567`), or `null` when it is not a valid mobile number.

### analyzePhone()

```ts
function analyzePhone(phone: string): PhoneAnalysis;

interface PhoneAnalysis {
  valid: boolean;
  formatted: string | null; // E.164
  network: string; // 'Jazz' | 'Zong' | 'Ufone' | 'Telenor' | 'SCO' | 'Unknown'
}
```

**Returns** validity, the E.164 form and the original network in one call.

### getNetwork()

```ts
function getNetwork(phone: string): string;
```

**Returns** `Jazz`, `Zong`, `Ufone`, `Telenor`, `SCO` (0355, Azad Jammu and Kashmir and Gilgit-Baltistan) or `Unknown`.

**Caveats.** This is the network the prefix was first allocated to. Pakistan has mobile number portability, so a number may have moved to another operator since.

**Related:** [`createWhatsAppLink()`](https://pakdatakit.dev/api/typescript/whatsapp/functions/createwhatsapplink/), [`phoneRiskSignals()`](https://pakdatakit.dev/api/typescript/quality/functions/phonerisksignals/).
