Phone numbers
NepaliPhoneNumber validates, parses, formats and finds Nepali mobile and landline numbers. It tells you the mobile operator (Nepal Telecom, Ncell, Smart Telecom, UTL), or the landline’s area code and district.
final phone = NepaliPhoneNumber.parse('+977 ९८४१२३४५६७');
phone.type; // PhoneType.mobilephone.operator; // MobileOperator.nepalTelecomphone.operator?.label(); // Nepal Telecomphone.format(); // 984-1234567phone.format(style: .international); // +977 984-1234567phone.format(language: .nepali); // ९८४-१२३४५६७phone.e164; // +9779841234567What is accepted
Section titled “What is accepted”- ASCII or Devanagari digits, or a mix of the two.
- An optional country code:
+977,00977or977. After the country code, the leading0of a landline can be written or left out. - Spaces,
-,.,(and)anywhere between the digits.
NepaliPhoneNumber.parse('977 9801234567').format(); // 980-1234567NepaliPhoneNumber.parse('(+977) 980.123.4567').format(); // 980-1234567NepaliPhoneNumber.parse('+977 1 4412345').format(); // 01-4412345NepaliPhoneNumber.parse('00977-1-4412345').format(); // 01-4412345NepaliPhoneNumber.parse('+977-01-4412345').format(); // 01-4412345Number types
Section titled “Number types”| Type | Shape | Example |
|---|---|---|
PhoneType.mobile |
10 digits; the first three are a prefix allocated to an operator | 984-1234567 |
PhoneType.landline, Kathmandu Valley |
01 + 7 digits |
01-4412345 |
PhoneType.landline, elsewhere |
0 + 2-digit area code + 6 digits |
061-523456 |
For a landline, the digit right after the area code is the operator code, and must be 4, 5 or 6. So 01-2412345 and 061-123456 aren’t valid.
Mobile operators
Section titled “Mobile operators”The prefixes come from the Nepal Telecommunications Authority’s National Numbering Allocation Plan, 2079:
MobileOperator |
Prefixes | label() / label(.nepali) |
|---|---|---|
nepalTelecom |
974, 975, 976, 984, 985, 986 | Nepal Telecom / नेपाल टेलिकम |
ncell |
970, 980, 981, 982 | Ncell / एनसेल |
smart |
961, 962, 988 | Smart Telecom / स्मार्ट टेलिकम |
utl |
972 | UTL / युटिएल |
NepaliPhoneNumber.parse('9801234567').operator?.label(); // NcellMobileOperator.forPrefix('984'); // MobileOperator.nepalTelecomMobileOperator.forPrefix('990'); // nullMobileOperator.nepalTelecom.prefixes; // [974, 975, 976, 984, 985, 986]A 10-digit number whose prefix isn’t allocated to any operator, such as 990…, isn’t valid.
Landline area codes
Section titled “Landline area codes”final office = NepaliPhoneNumber.parse('061-523456');
office.type; // PhoneType.landlineoffice.areaCode; // 061office.areaName(); // Kaskioffice.areaName(.nepali); // कास्कीoffice.operator; // null
NepaliPhoneNumber.parse('01-4412345').areaName(); // Kathmandu ValleyNepaliPhoneNumber.parse('021-523456').areaName(); // MorangareaCode is null for mobile numbers. areaName returns null for mobile numbers and for area codes that aren’t in its list. Some codes cover more than one district, for example 026, which is Dhankuta and Terhathum.
Validating
Section titled “Validating”NepaliPhoneNumber.isValid('9801234567'); // trueNepaliPhoneNumber.isValid('9901234567'); // falseNepaliPhoneNumber.isMobile('01-4412345'); // falseNepaliPhoneNumber.isLandline('01-4412345'); // trueNepaliPhoneNumber.isValid('9801234567', type: .landline); // false
NepaliPhoneNumber.tryParse('9901234567'); // nullNepaliPhoneNumber.parse('12345'); // throws FormatException: Invalid phone numberIn a Flutter form:
TextFormField( keyboardType: TextInputType.phone, validator: (value) => NepaliPhoneNumber.isMobile(value ?? '') ? null : 'Enter a valid mobile number',);Formatting
Section titled “Formatting”format({PhoneFormat style = .national, Language? language}):
PhoneFormat |
Mobile | Kathmandu landline | Other landline |
|---|---|---|---|
.national |
984-1234567 |
01-4412345 |
061-523456 |
.international |
+977 984-1234567 |
+977 1-4412345 |
+977 61-523456 |
.e164 |
+9779841234567 |
+97714412345 |
+97761523456 |
With language: .nepali, digits are Devanagari:
phone.format(language: .nepali); // ९८४-१२३४५६७phone.format(style: .international, language: .nepali); // +९७७ ९८४-१२३४५६७language defaults to NepaliUtils.currentLanguage. toString() always uses English national format.
Other getters:
phone.nationalNumber; // 9841234567 (as dialled in Nepal, no separators)phone.e164; // +9779841234567 (same as format(style: .e164))Equality
Section titled “Equality”Two numbers are equal when their nationalNumber is the same, however they were written:
NepaliPhoneNumber.parse('+977-9841234567') == NepaliPhoneNumber.parse('९८४१२३४५६७'); // trueStore e164 or nationalNumber in your database. Don’t store the text as the user typed it.
Finding numbers in text
Section titled “Finding numbers in text”findAll returns every number in a text, in order, as PhoneNumberMatch objects:
for (final match in NepaliPhoneNumber.findAll('Call 9841234567 or ०१-४४१२३४५.')) { print('${match.text} at ${match.start}-${match.end}: ${match.number.e164}');}// 9841234567 at 5-15: +9779841234567// ०१-४४१२३४५ at 19-29: +97714412345PhoneNumberMatch |
Meaning |
|---|---|
start, end |
position of the number in the text |
text |
the number exactly as written |
number |
the parsed NepaliPhoneNumber |
Pass type to find only mobile or only landline numbers:
NepaliPhoneNumber.findAll('Call 9841234567 or 01-4412345.', type: .mobile) .map((match) => match.number.format()) .toList(); // [984-1234567]A number isn’t matched if it comes right after a digit, a Latin letter or +, or right before a digit. Devanagari text may follow it directly:
NepaliPhoneNumber.findAll('ID9841234567 and 98412345678').toList(); // []NepaliPhoneNumber.findAll('९८४१२३४५६७मा फोन गर्नुहोस्') .map((match) => match.text) .toList(); // [९८४१२३४५६७]Replacing numbers
Section titled “Replacing numbers”replaceAll replaces every match with the result of a callback. This is useful for masking numbers or making them consistent:
NepaliPhoneNumber.replaceAll( 'Call 9841234567', (match) => '${match.text.substring(0, 3)}*******',); // Call 984*******
NepaliPhoneNumber.replaceAll( 'Call 9841234567 or 014412345', (match) => match.number.format(style: .international),); // Call +977 984-1234567 or +977 1-4412345