global-phone-validator 1.2.1 → 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 +143 -78
- package/dist/countryData.d.ts +24 -0
- package/dist/countryData.d.ts.map +1 -0
- package/dist/countryData.js +1015 -0
- package/dist/countryData.js.map +1 -0
- package/dist/dataAccess.d.ts +35 -0
- package/dist/dataAccess.d.ts.map +1 -0
- package/dist/dataAccess.js +92 -0
- package/dist/dataAccess.js.map +1 -0
- package/dist/index.d.ts +0 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -95
- package/dist/index.js.map +1 -1
- package/dist/mobilePrefixes.d.ts +3 -10
- package/dist/mobilePrefixes.d.ts.map +1 -1
- package/dist/mobilePrefixes.js +372 -143
- package/dist/mobilePrefixes.js.map +1 -1
- package/dist/phoneTypes.d.ts +10 -0
- package/dist/phoneTypes.d.ts.map +1 -0
- package/dist/phoneTypes.js +63 -0
- package/dist/phoneTypes.js.map +1 -0
- package/dist/types.d.ts +2 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +0 -1
- package/dist/utils.d.ts +0 -17
- package/dist/utils.js +12 -20
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
# global-phone-validator
|
|
2
2
|
|
|
3
|
-
A comprehensive Node.js + TypeScript package for validating **
|
|
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
|
-
- ✅
|
|
8
|
-
- ✅
|
|
9
|
-
- ✅
|
|
10
|
-
- ✅
|
|
11
|
-
- ✅
|
|
12
|
-
- ✅
|
|
13
|
-
- ✅
|
|
14
|
-
- ✅
|
|
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
|
|
|
@@ -108,18 +108,20 @@ console.log(result4);
|
|
|
108
108
|
// }
|
|
109
109
|
```
|
|
110
110
|
|
|
111
|
-
###
|
|
111
|
+
### Phone Type Detection
|
|
112
112
|
|
|
113
113
|
```typescript
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
114
|
+
validatePhoneNumber("+919876543210").phoneType; // 'mobile'
|
|
115
|
+
validatePhoneNumber("+442079460958").phoneType; // 'landline'
|
|
116
|
+
validatePhoneNumber("+18001234567").phoneType; // 'toll-free'
|
|
117
|
+
validatePhoneNumber("+19001234567").phoneType; // 'premium'
|
|
118
|
+
```
|
|
117
119
|
|
|
118
|
-
|
|
119
|
-
console.log(result2.isValid); // true (German mobile number)
|
|
120
|
+
### Mobile-Only Validation
|
|
120
121
|
|
|
121
|
-
|
|
122
|
-
|
|
122
|
+
```typescript
|
|
123
|
+
validatePhoneNumber("9876543210", "IN", true); // true (mobile)
|
|
124
|
+
validatePhoneNumber("0123456789", "IN", true); // false (landline)
|
|
123
125
|
```
|
|
124
126
|
|
|
125
127
|
### Get Country Codes
|
|
@@ -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,55 +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
|
-
//
|
|
306
|
-
validatePhoneNumber("+
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
//
|
|
310
|
-
validatePhoneNumber("+
|
|
311
|
-
//
|
|
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 mobile number
|
|
322
|
-
validatePhoneNumber("+44 7123 456789");
|
|
323
|
-
// { isValid: true, country: 'GB', isMobile: true, e164: '+447123456789', ... }
|
|
324
|
-
|
|
325
|
-
// Germany mobile number
|
|
326
|
-
validatePhoneNumber("+49 17677274194");
|
|
327
|
-
// { isValid: true, country: 'DE', isMobile: true, e164: '+4917677274194', ... }
|
|
328
|
-
|
|
329
|
-
// France number
|
|
330
|
-
validatePhoneNumber("+33 1 23 45 67 89");
|
|
331
|
-
// { isValid: true, country: 'FR', e164: '+33123456789', ... }
|
|
332
|
-
|
|
333
|
-
// Australia number
|
|
334
|
-
validatePhoneNumber("+61 2 1234 5678");
|
|
335
|
-
// { isValid: true, country: 'AU', e164: '+61212345678', ... }
|
|
336
|
-
|
|
337
|
-
// Brazil number
|
|
338
|
-
validatePhoneNumber("+55 11 98765 4321");
|
|
339
|
-
// { isValid: true, country: 'BR', e164: '+5511987654321', ... }
|
|
340
|
-
|
|
341
|
-
// China number
|
|
342
|
-
validatePhoneNumber("+86 138 0013 8000");
|
|
343
|
-
// { isValid: true, country: 'CN', e164: '+8613800138000', ... }
|
|
344
|
-
|
|
345
|
-
// Japan number
|
|
346
|
-
validatePhoneNumber("+81 3 1234 5678");
|
|
347
|
-
// { isValid: true, country: 'JP', e164: '+81312345678', ... }
|
|
348
|
-
|
|
349
|
-
// Germany number with different spacing (all formats work)
|
|
350
|
-
validatePhoneNumber("+49 17677274194"); // With space after country code
|
|
351
|
-
validatePhoneNumber("+49 176 77274194"); // With spaces in number
|
|
352
|
-
validatePhoneNumber("+4917677274194"); // No spaces
|
|
353
|
-
// 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
|
|
354
327
|
```
|
|
355
328
|
|
|
356
329
|
## Why Use This Package?
|
|
@@ -375,17 +348,113 @@ npm run build
|
|
|
375
348
|
npm test
|
|
376
349
|
```
|
|
377
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
|
+
|
|
378
449
|
## Contributing
|
|
379
450
|
|
|
380
451
|
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
381
452
|
|
|
382
|
-
###
|
|
383
|
-
|
|
384
|
-
If you'd like to add or improve validation rules for specific countries, please:
|
|
453
|
+
### Contributing
|
|
385
454
|
|
|
386
455
|
1. Fork the repository
|
|
387
|
-
2. Add
|
|
388
|
-
3.
|
|
456
|
+
2. Add validation rules to `src/index.ts`
|
|
457
|
+
3. Add phone type rules to `src/phoneTypes.ts`
|
|
389
458
|
4. Submit a Pull Request
|
|
390
459
|
|
|
391
460
|
## License
|
|
@@ -394,10 +463,6 @@ MIT
|
|
|
394
463
|
|
|
395
464
|
## Links
|
|
396
465
|
|
|
397
|
-
-
|
|
398
|
-
-
|
|
399
|
-
-
|
|
400
|
-
|
|
401
|
-
## License
|
|
402
|
-
|
|
403
|
-
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"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,MAAM,WAAW,WAAW;IAE1B,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IAGb,UAAU,CAAC,EAAE;QACX,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;QAC1B,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,CAAC;IAGF,UAAU,CAAC,EAAE,cAAc,CAAC;IAG5B,QAAQ,CAAC,EAAE;QACT,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,iBAAiB,CAAC,EAAE,OAAO,CAAC;QAC5B,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB,CAAC;CACH;AAOD;;;GAGG;AACH,eAAO,MAAM,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAgGpD,CAAC;AAKF;;;;GAIG;AACH,wBAAgB,qBAAqB,IAAI,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CA8CnE;AAED;;GAEG;AACH,wBAAgB,qBAAqB,IAAI,IAAI,CAE5C;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,WAAW,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS,CAG3E;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS,CAGhF;AAED;;GAEG;AACH,wBAAgB,iBAAiB,IAAI,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAE/D;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,WAAW,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,WAAW,CAAC,GAAG,IAAI,CAkBpF"}
|