global-phone-validator 1.2.0 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,17 +1,17 @@
1
1
  # global-phone-validator
2
2
 
3
- A comprehensive Node.js + TypeScript package for validating phone numbers worldwide. Supports **all countries** with **country-specific validation rules** for 50+ major countries. Handles international formats (+CC), 0-prefixed, and plain digits. Includes mobile/landline detection for India and returns standardized E.164 format.
3
+ A comprehensive Node.js + TypeScript package for validating **all types of phone numbers** worldwide. Supports **all countries** with **country-specific validation rules** and **phone number type detection** (mobile, landline, VoIP, toll-free, premium, special services) for 30+ major countries. Handles international formats (+CC), 0-prefixed, and plain digits. Returns standardized E.164 format.
4
4
 
5
5
  ## Features
6
6
 
7
- - ✅ **True Global Validation**: Validates phone numbers for **all countries** with country-specific rules for 50+ major countries
8
- - ✅ **Country-Specific Rules**: Accurate validation for US, UK, Germany, France, Australia, Brazil, China, Japan, India, and 40+ more countries
9
- - ✅ **Multiple Formats**: Handles international format (+CC), 0-prefixed, and plain digit formats
10
- - ✅ **India-Specific**: Mobile vs landline detection for Indian phone numbers
11
- - ✅ **Standardized Output**: Returns E.164 format (+CCNNNNNNNNN) with country information
12
- - ✅ **TypeScript**: Fully typed with TypeScript definitions
13
- - ✅ **Zero Dependencies**: No external dependencies required
14
- - ✅ **ITU-T E.164 Compliant**: Follows international telecommunication standards
7
+ - ✅ Global validation for all countries
8
+ - ✅ Phone type detection (mobile, landline, VoIP, toll-free, premium)
9
+ - ✅ 50+ countries with specific validation rules
10
+ - ✅ 70+ countries with mobile prefix detection
11
+ - ✅ Multiple input formats (+CC, 0-prefixed, plain digits)
12
+ - ✅ E.164 format output
13
+ - ✅ Full TypeScript support
14
+ - ✅ Zero dependencies
15
15
 
16
16
  ## Installation
17
17
 
@@ -38,12 +38,11 @@ const { validatePhoneNumber } = require("global-phone-validator");
38
38
 
39
39
  // Test various countries
40
40
  console.log(validatePhoneNumber("+91 98765 43210")); // India - Mobile
41
- console.log(validatePhoneNumber("+1 555 123 4567")); // USA
42
- console.log(validatePhoneNumber("+44 20 7946 0958")); // UK
41
+ console.log(validatePhoneNumber("+1 555 123 4567")); // USA - Mobile
42
+ console.log(validatePhoneNumber("+44 7123 456789")); // UK - Mobile
43
43
  console.log(validatePhoneNumber("+49 176 77274194")); // Germany - Mobile
44
- console.log(validatePhoneNumber("+49 30 12345678")); // Germany - Landline
45
- console.log(validatePhoneNumber("+61 2 1234 5678")); // Australia
46
- console.log(validatePhoneNumber("+86 138 0013 8000")); // China
44
+ console.log(validatePhoneNumber("+61 4 1234 5678")); // Australia - Mobile
45
+ console.log(validatePhoneNumber("+86 138 0013 8000")); // China - Mobile
47
46
  ```
48
47
 
49
48
  ### Test Locally
@@ -72,7 +71,6 @@ console.log(result1);
72
71
  // nationalNumber: '9876543210',
73
72
  // e164: '+919876543210',
74
73
  // isMobile: true,
75
- // isFixedLine: false,
76
74
  // country: 'IN',
77
75
  // countryName: 'India'
78
76
  // }
@@ -110,15 +108,20 @@ console.log(result4);
110
108
  // }
111
109
  ```
112
110
 
113
- ### Mobile-Only Validation (India)
111
+ ### Phone Type Detection
114
112
 
115
113
  ```typescript
116
- // Only accept mobile numbers
117
- const result = validatePhoneNumber("9876543210", "IN", true);
118
- console.log(result.isValid); // true (mobile number)
114
+ validatePhoneNumber("+919876543210").phoneType; // 'mobile'
115
+ validatePhoneNumber("+442079460958").phoneType; // 'landline'
116
+ validatePhoneNumber("+18001234567").phoneType; // 'toll-free'
117
+ validatePhoneNumber("+19001234567").phoneType; // 'premium'
118
+ ```
119
+
120
+ ### Mobile-Only Validation
119
121
 
120
- const result2 = validatePhoneNumber("0123456789", "IN", true);
121
- console.log(result2.isValid); // false (landline number)
122
+ ```typescript
123
+ validatePhoneNumber("9876543210", "IN", true); // true (mobile)
124
+ validatePhoneNumber("0123456789", "IN", true); // false (landline)
122
125
  ```
123
126
 
124
127
  ### Get Country Codes
@@ -158,7 +161,7 @@ Validates a phone number and returns detailed information.
158
161
  - If `defaultCountry` is provided: Validates that detected country **matches** `defaultCountry` (strict mode)
159
162
  - If `defaultCountry` is **not provided**: Validates based on the **detected country** from the dial code
160
163
  - **When number doesn't have `+` prefix**: `defaultCountry` is **REQUIRED** - no default country is assumed
161
- - `mobileOnly` (boolean, optional): If true, only accepts mobile numbers (currently only for India). Default: false
164
+ - `mobileOnly` (boolean, optional): If true, only accepts mobile numbers (works for all countries with mobile prefix rules). Default: false
162
165
 
163
166
  **Returns:** `PhoneValidationResult`
164
167
 
@@ -168,8 +171,7 @@ interface PhoneValidationResult {
168
171
  countryCode?: string; // e.g., '91'
169
172
  nationalNumber?: string; // e.g., '9876543210'
170
173
  e164?: string; // e.g., '+919876543210'
171
- isMobile?: boolean; // true if mobile (India only)
172
- isFixedLine?: boolean; // true if landline (India only)
174
+ isMobile?: boolean; // true if mobile number (based on country-specific prefix rules)
173
175
  country?: string; // ISO country code, e.g., 'IN'
174
176
  countryName?: string; // Full country name, e.g., 'India'
175
177
  }
@@ -261,7 +263,7 @@ The package includes **comprehensive validation rules** for **50+ major countrie
261
263
 
262
264
  #### Asia-Pacific
263
265
 
264
- - **India (91)**: 10 digits (mobile: 6-9, landline: 0-5) - Mobile/landline detection
266
+ - **India (91)**: 10 digits (mobile: 6-9) - Mobile prefix detection
265
267
  - **China (86)**: 11 digits
266
268
  - **Japan (81)**: 10-11 digits
267
269
  - **South Korea (82)**: 9-10 digits
@@ -291,6 +293,19 @@ The package includes **comprehensive validation rules** for **50+ major countrie
291
293
  - **Qatar (974)**: 8 digits
292
294
  - **And more...**
293
295
 
296
+ ### Mobile Prefix Detection
297
+
298
+ The package includes **mobile prefix detection** for **70+ countries**, allowing automatic identification of mobile numbers:
299
+
300
+ **Countries with Mobile Prefix Detection:**
301
+
302
+ - **Europe**: Germany, UK, France, Italy, Spain, Netherlands, Sweden, Norway, Poland, Russia, Austria, Belgium, Switzerland, Portugal, Greece, Ireland, Czech Republic, Denmark, Finland, Hungary, Iceland, Romania, Slovakia, Slovenia, Ukraine, and more
303
+ - **Asia-Pacific**: India, China, Japan, South Korea, Australia, Indonesia, Philippines, Thailand, Malaysia, Singapore, New Zealand, Vietnam, Pakistan, Bangladesh, Sri Lanka, Myanmar, Iran, Israel, Hong Kong, Taiwan, and more
304
+ - **Americas**: Argentina, Chile, Colombia, Peru, Venezuela, Uruguay, and more
305
+ - **Africa & Middle East**: South Africa, Nigeria, UAE, Egypt, Morocco, Qatar, Kenya, and more
306
+
307
+ **Note:** Some countries (like Mexico, Brazil) have area-code-dependent mobile prefixes that require area code information for accurate detection. For these countries, format validation is performed but mobile detection may be limited.
308
+
294
309
  ### Other Countries
295
310
 
296
311
  - **General validation**: 4-15 digits (ITU-T E.164 standard)
@@ -302,59 +317,13 @@ The package includes **comprehensive validation rules** for **50+ major countrie
302
317
  ```typescript
303
318
  import { validatePhoneNumber } from "global-phone-validator";
304
319
 
305
- // Valid Indian mobile number
306
- validatePhoneNumber("+91 98765 43210");
307
- // { isValid: true, isMobile: true, e164: '+919876543210', ... }
308
-
309
- // Valid Indian landline
310
- validatePhoneNumber("0123456789", "IN");
311
- // { isValid: true, isFixedLine: true, ... }
312
-
313
- // Invalid number
314
- validatePhoneNumber("12345");
315
- // { isValid: false }
316
-
317
- // US number
318
- validatePhoneNumber("+1 555 123 4567");
319
- // { isValid: true, country: 'US', e164: '+15551234567', ... }
320
-
321
- // UK number
322
- validatePhoneNumber("+44 20 7946 0958");
323
- // { isValid: true, country: 'GB', e164: '+442079460958', ... }
324
-
325
- // Germany number (mobile)
326
- validatePhoneNumber("+49 17677274194");
327
- // { isValid: true, country: 'DE', e164: '+4917677274194', ... }
328
-
329
- // Germany number (landline)
330
- validatePhoneNumber("+49 30 12345678");
331
- // { isValid: true, country: 'DE', e164: '+493012345678', ... }
332
-
333
- // France number
334
- validatePhoneNumber("+33 1 23 45 67 89");
335
- // { isValid: true, country: 'FR', e164: '+33123456789', ... }
336
-
337
- // Australia number
338
- validatePhoneNumber("+61 2 1234 5678");
339
- // { isValid: true, country: 'AU', e164: '+61212345678', ... }
340
-
341
- // Brazil number
342
- validatePhoneNumber("+55 11 98765 4321");
343
- // { isValid: true, country: 'BR', e164: '+5511987654321', ... }
344
-
345
- // China number
346
- validatePhoneNumber("+86 138 0013 8000");
347
- // { isValid: true, country: 'CN', e164: '+8613800138000', ... }
348
-
349
- // Japan number
350
- validatePhoneNumber("+81 3 1234 5678");
351
- // { isValid: true, country: 'JP', e164: '+81312345678', ... }
352
-
353
- // Germany number with different spacing (all formats work)
354
- validatePhoneNumber("+49 17677274194"); // With space after country code
355
- validatePhoneNumber("+49 176 77274194"); // With spaces in number
356
- validatePhoneNumber("+4917677274194"); // No spaces
357
- // All produce: { isValid: true, country: 'DE', nationalNumber: '17677274194', e164: '+4917677274194', ... }
320
+ validatePhoneNumber("+91 98765 43210"); // India mobile
321
+ validatePhoneNumber("+1 555 123 4567"); // US
322
+ validatePhoneNumber("+44 7123 456789"); // UK mobile
323
+ validatePhoneNumber("+49 17677274194"); // Germany mobile
324
+ validatePhoneNumber("+18001234567"); // US toll-free
325
+ validatePhoneNumber("+19001234567"); // US premium
326
+ validatePhoneNumber("12345"); // Invalid
358
327
  ```
359
328
 
360
329
  ## Why Use This Package?
@@ -379,17 +348,113 @@ npm run build
379
348
  npm test
380
349
  ```
381
350
 
351
+ ## Data Structure
352
+
353
+ The package uses a **generalized, unified data structure** that makes it easy to extend and maintain:
354
+
355
+ ### Country Data Structure
356
+
357
+ All country information is consolidated into a single `CountryData` interface:
358
+
359
+ ```typescript
360
+ interface CountryData {
361
+ name: string; // Country name
362
+ dial_code: string; // Dial code with + (e.g., "+91")
363
+ code: string; // ISO country code (e.g., "IN")
364
+
365
+ validation?: {
366
+ // Optional validation rules
367
+ pattern?: RegExp | string;
368
+ minLength?: number;
369
+ maxLength?: number;
370
+ };
371
+
372
+ phoneTypes?: {
373
+ // Phone type detection rules
374
+ mobilePrefixes?: string[];
375
+ landlinePrefixes?: string[];
376
+ voipPrefixes?: string[];
377
+ tollFreePrefixes?: string[];
378
+ premiumPrefixes?: string[];
379
+ specialPrefixes?: string[];
380
+ };
381
+
382
+ metadata?: {
383
+ // Optional metadata
384
+ notes?: string;
385
+ areaCodeDependent?: boolean;
386
+ lastUpdated?: string;
387
+ };
388
+ }
389
+ ```
390
+
391
+ ### Data Sources
392
+
393
+ 1. **CountryCodes.json**: Basic country information (name, dial_code, code)
394
+ 2. **phoneTypes.ts**: Phone type detection rules (mobile, landline, VoIP, etc.)
395
+ 3. **countryData.ts**: Unified structure that merges both sources
396
+
397
+ ### Extending the Data
398
+
399
+ You can easily add or update country data:
400
+
401
+ ```typescript
402
+ import { addCountryData } from "global-phone-validator/dataAccess";
403
+
404
+ // Add new country with phone type rules
405
+ addCountryData("123", {
406
+ name: "New Country",
407
+ dial_code: "+123",
408
+ code: "NC",
409
+ validation: {
410
+ pattern: "^\\d{10}$",
411
+ minLength: 10,
412
+ maxLength: 10,
413
+ },
414
+ phoneTypes: {
415
+ mobilePrefixes: ["9"],
416
+ landlinePrefixes: ["1", "2", "3"],
417
+ tollFreePrefixes: ["800"],
418
+ },
419
+ });
420
+ ```
421
+
422
+ ### Data Access API
423
+
424
+ ```typescript
425
+ import {
426
+ getCountryData,
427
+ getPhoneTypeRules,
428
+ getValidationRules,
429
+ getCountriesWithPhoneTypeSupport,
430
+ hasPhoneTypeSupport,
431
+ } from "global-phone-validator/dataAccess";
432
+
433
+ // Get complete country data
434
+ const india = getCountryData("91");
435
+
436
+ // Get phone type rules only
437
+ const rules = getPhoneTypeRules("91");
438
+
439
+ // Get validation rules only
440
+ const validation = getValidationRules("91");
441
+
442
+ // Get all countries with phone type support
443
+ const countries = getCountriesWithPhoneTypeSupport();
444
+
445
+ // Check if country supports specific phone type
446
+ const hasMobile = hasPhoneTypeSupport("91", "mobile");
447
+ ```
448
+
382
449
  ## Contributing
383
450
 
384
451
  Contributions are welcome! Please feel free to submit a Pull Request.
385
452
 
386
- ### Adding Country Validation Rules
387
-
388
- If you'd like to add or improve validation rules for specific countries, please:
453
+ ### Contributing
389
454
 
390
455
  1. Fork the repository
391
- 2. Add the validation rule to the `validationRules` object in `src/index.ts`
392
- 3. Update the README with the country information
456
+ 2. Add validation rules to `src/index.ts`
457
+ 3. Add phone type rules to `src/phoneTypes.ts`
393
458
  4. Submit a Pull Request
394
459
 
395
460
  ## License
@@ -398,10 +463,6 @@ MIT
398
463
 
399
464
  ## Links
400
465
 
401
- - **NPM Package**: [global-phone-validator](https://www.npmjs.com/package/global-phone-validator)
402
- - **GitHub Repository**: [AVantala/global-phone-validator](https://github.com/AVantala/global-phone-validator)
403
- - **Issues**: [Report a bug or request a feature](https://github.com/AVantala/global-phone-validator/issues)
404
-
405
- ## License
406
-
407
- MIT
466
+ - [NPM Package](https://www.npmjs.com/package/global-phone-validator)
467
+ - [GitHub Repository](https://github.com/AVantala/global-phone-validator)
468
+ - [Report Issues](https://github.com/AVantala/global-phone-validator/issues)
@@ -0,0 +1,24 @@
1
+ import { PhoneTypeRules } from "./phoneTypes";
2
+ export interface CountryData {
3
+ name: string;
4
+ dial_code: string;
5
+ code: string;
6
+ validation?: {
7
+ pattern?: RegExp | string;
8
+ minLength?: number;
9
+ maxLength?: number;
10
+ };
11
+ phoneTypes?: PhoneTypeRules;
12
+ metadata?: {
13
+ notes?: string;
14
+ areaCodeDependent?: boolean;
15
+ lastUpdated?: string;
16
+ };
17
+ }
18
+ export declare const COUNTRY_DATA: Record<string, Partial<CountryData>>;
19
+ export declare function initializeCountryData(): Record<string, CountryData>;
20
+ export declare function clearCountryDataCache(): void;
21
+ export declare function getCountryData(countryCode: string): CountryData | undefined;
22
+ export declare function getCountryDataByIsoCode(isoCode: string): CountryData | undefined;
23
+ export declare function getAllCountryData(): Record<string, CountryData>;
24
+ export declare function setCountryData(countryCode: string, data: Partial<CountryData>): void;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"countryData.d.ts","sourceRoot":"","sources":["../src/countryData.ts"],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