global-phone-validator 1.1.0 → 1.1.1

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.
Files changed (2) hide show
  1. package/README.md +167 -43
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,15 +1,17 @@
1
1
  # global-phone-validator
2
2
 
3
- A reusable Node.js + TypeScript package for validating phone numbers worldwide. Supports any country using CountryCodes JSON, 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 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.
4
4
 
5
5
  ## Features
6
6
 
7
- - ✅ **Worldwide Support**: Validates phone numbers for any country using CountryCodes JSON
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
8
9
  - ✅ **Multiple Formats**: Handles international format (+CC), 0-prefixed, and plain digit formats
9
10
  - ✅ **India-Specific**: Mobile vs landline detection for Indian phone numbers
10
- - ✅ **Standardized Output**: Returns E.164 format (+CCNNNNNNNNN)
11
+ - ✅ **Standardized Output**: Returns E.164 format (+CCNNNNNNNNN) with country information
11
12
  - ✅ **TypeScript**: Fully typed with TypeScript definitions
12
13
  - ✅ **Zero Dependencies**: No external dependencies required
14
+ - ✅ **ITU-T E.164 Compliant**: Follows international telecommunication standards
13
15
 
14
16
  ## Installation
15
17
 
@@ -19,15 +21,33 @@ npm install global-phone-validator
19
21
  yarn add global-phone-validator
20
22
  ```
21
23
 
24
+ ## Try It Online
25
+
26
+ Test the package directly in your browser with RunKit:
27
+
28
+ **[▶️ Try on RunKit](https://npm.runkit.com/global-phone-validator)**
29
+
30
+ Or use this code snippet:
31
+
32
+ ```javascript
33
+ const { validatePhoneNumber } = require("global-phone-validator");
34
+
35
+ // Test various countries
36
+ validatePhoneNumber("+91 98765 43210"); // India
37
+ validatePhoneNumber("+1 555 123 4567"); // USA
38
+ validatePhoneNumber("+44 20 7946 0958"); // UK
39
+ validatePhoneNumber("+49 30 12345678"); // Germany
40
+ ```
41
+
22
42
  ## Usage
23
43
 
24
44
  ### Basic Validation
25
45
 
26
46
  ```typescript
27
- import { validatePhoneNumber } from 'global-phone-validator';
47
+ import { validatePhoneNumber } from "global-phone-validator";
28
48
 
29
49
  // International format with country code
30
- const result1 = validatePhoneNumber('+91 98765 43210');
50
+ const result1 = validatePhoneNumber("+91 98765 43210");
31
51
  console.log(result1);
32
52
  // {
33
53
  // isValid: true,
@@ -41,15 +61,15 @@ console.log(result1);
41
61
  // }
42
62
 
43
63
  // 0-prefixed format (India)
44
- const result2 = validatePhoneNumber('09876543210');
64
+ const result2 = validatePhoneNumber("09876543210");
45
65
  console.log(result2.isValid); // true
46
66
 
47
67
  // Plain digits with default country
48
- const result3 = validatePhoneNumber('9876543210', 'IN');
68
+ const result3 = validatePhoneNumber("9876543210", "IN");
49
69
  console.log(result3.isValid); // true
50
70
 
51
71
  // US number
52
- const result4 = validatePhoneNumber('+1 555 123 4567');
72
+ const result4 = validatePhoneNumber("+1 555 123 4567");
53
73
  console.log(result4);
54
74
  // {
55
75
  // isValid: true,
@@ -65,28 +85,32 @@ console.log(result4);
65
85
 
66
86
  ```typescript
67
87
  // Only accept mobile numbers
68
- const result = validatePhoneNumber('9876543210', 'IN', true);
88
+ const result = validatePhoneNumber("9876543210", "IN", true);
69
89
  console.log(result.isValid); // true (mobile number)
70
90
 
71
- const result2 = validatePhoneNumber('0123456789', 'IN', true);
91
+ const result2 = validatePhoneNumber("0123456789", "IN", true);
72
92
  console.log(result2.isValid); // false (landline number)
73
93
  ```
74
94
 
75
95
  ### Get Country Codes
76
96
 
77
97
  ```typescript
78
- import { getAllCountryCodes, getCountryDialCode, getCountryCodeByDialCode } from 'global-phone-validator';
98
+ import {
99
+ getAllCountryCodes,
100
+ getCountryDialCode,
101
+ getCountryCodeByDialCode,
102
+ } from "global-phone-validator";
79
103
 
80
104
  // Get all country codes
81
105
  const countries = getAllCountryCodes();
82
106
  console.log(countries); // Array of all countries
83
107
 
84
108
  // Get dial code for a country
85
- const dialCode = getCountryDialCode('IN');
109
+ const dialCode = getCountryDialCode("IN");
86
110
  console.log(dialCode); // '91'
87
111
 
88
112
  // Get country by dial code
89
- const country = getCountryCodeByDialCode('91');
113
+ const country = getCountryCodeByDialCode("91");
90
114
  console.log(country); // { name: 'India', dial_code: '+91', code: 'IN' }
91
115
  ```
92
116
 
@@ -97,6 +121,7 @@ console.log(country); // { name: 'India', dial_code: '+91', code: 'IN' }
97
121
  Validates a phone number and returns detailed information.
98
122
 
99
123
  **Parameters:**
124
+
100
125
  - `input` (string): Phone number in various formats
101
126
  - `defaultCountry` (string, optional): ISO country code (e.g., "IN", "US") used when country cannot be detected. Default: "IN"
102
127
  - `mobileOnly` (boolean, optional): If true, only accepts mobile numbers (currently only for India). Default: false
@@ -106,13 +131,13 @@ Validates a phone number and returns detailed information.
106
131
  ```typescript
107
132
  interface PhoneValidationResult {
108
133
  isValid: boolean;
109
- countryCode?: string; // e.g., '91'
110
- nationalNumber?: string; // e.g., '9876543210'
111
- e164?: string; // e.g., '+919876543210'
112
- isMobile?: boolean; // true if mobile (India only)
113
- isFixedLine?: boolean; // true if landline (India only)
114
- country?: string; // ISO country code, e.g., 'IN'
115
- countryName?: string; // Full country name, e.g., 'India'
134
+ countryCode?: string; // e.g., '91'
135
+ nationalNumber?: string; // e.g., '9876543210'
136
+ e164?: string; // e.g., '+919876543210'
137
+ isMobile?: boolean; // true if mobile (India only)
138
+ isFixedLine?: boolean; // true if landline (India only)
139
+ country?: string; // ISO country code, e.g., 'IN'
140
+ countryName?: string; // Full country name, e.g., 'India'
116
141
  }
117
142
  ```
118
143
 
@@ -124,9 +149,9 @@ Returns an array of all supported countries.
124
149
 
125
150
  ```typescript
126
151
  interface CountryCode {
127
- name: string; // e.g., 'India'
128
- dial_code: string; // e.g., '+91'
129
- code: string; // e.g., 'IN'
152
+ name: string; // e.g., 'India'
153
+ dial_code: string; // e.g., '+91'
154
+ code: string; // e.g., 'IN'
130
155
  }
131
156
  ```
132
157
 
@@ -135,6 +160,7 @@ interface CountryCode {
135
160
  Gets the dial code for a country.
136
161
 
137
162
  **Parameters:**
163
+
138
164
  - `countryCode` (string): ISO country code (e.g., "IN")
139
165
 
140
166
  **Returns:** `string | null` - The dial code without the + sign (e.g., "91")
@@ -144,6 +170,7 @@ Gets the dial code for a country.
144
170
  Gets country information by dial code.
145
171
 
146
172
  **Parameters:**
173
+
147
174
  - `dialCode` (string): Dial code with or without + (e.g., "91" or "+91")
148
175
 
149
176
  **Returns:** `CountryCode | null`
@@ -158,60 +185,139 @@ The package handles phone numbers in the following formats:
158
185
 
159
186
  ## Country-Specific Validation
160
187
 
161
- The package includes validation rules for **50+ major countries** with specific length and format requirements:
188
+ The package includes **comprehensive validation rules** for **50+ major countries** with specific length and format requirements. Each country has its own validation pattern ensuring accurate phone number validation.
162
189
 
163
190
  ### Countries with Specific Validation Rules
164
191
 
165
- - **India (91)**: 10 digits (mobile: 6-9, landline: 0-5) - Mobile/landline detection
192
+ #### Americas
193
+
166
194
  - **United States/Canada (1)**: 10 digits
195
+ - **Brazil (55)**: 10-11 digits
196
+ - **Mexico (52)**: 10 digits
197
+ - **Argentina (54)**: 10 digits
198
+ - **Chile (56)**: 9 digits
199
+ - **Colombia (57)**: 10 digits
200
+ - **Venezuela (58)**: 10 digits
201
+ - **Peru (51)**: 9 digits
202
+
203
+ #### Europe
204
+
167
205
  - **United Kingdom (44)**: 10-11 digits
168
206
  - **Germany (49)**: 10-11 digits
169
207
  - **France (33)**: 9 digits
170
- - **Australia (61)**: 9 digits
171
- - **Brazil (55)**: 10-11 digits
172
- - **China (86)**: 11 digits
173
- - **Japan (81)**: 10-11 digits
174
- - **South Korea (82)**: 9-10 digits
175
- - **Russia (7)**: 10 digits
176
- - **Spain (34)**: 9 digits
177
208
  - **Italy (39)**: 9-10 digits
209
+ - **Spain (34)**: 9 digits
178
210
  - **Netherlands (31)**: 9 digits
179
211
  - **Sweden (46)**: 9 digits
180
212
  - **Norway (47)**: 8 digits
181
213
  - **Poland (48)**: 9 digits
182
- - **Turkey (90)**: 10 digits
214
+ - **Russia (7)**: 10 digits
215
+ - **Switzerland (41)**: 9 digits
216
+ - **Belgium (32)**: 9 digits
217
+ - **Greece (30)**: 10 digits
218
+ - **Portugal (351)**: 9 digits
219
+ - **Ireland (353)**: 9 digits
220
+ - **Czech Republic (420)**: 9 digits
221
+ - **Ukraine (380)**: 9 digits
222
+ - **And more...**
223
+
224
+ #### Asia-Pacific
225
+
226
+ - **India (91)**: 10 digits (mobile: 6-9, landline: 0-5) - Mobile/landline detection
227
+ - **China (86)**: 11 digits
228
+ - **Japan (81)**: 10-11 digits
229
+ - **South Korea (82)**: 9-10 digits
230
+ - **Australia (61)**: 9 digits
231
+ - **Indonesia (62)**: 9-11 digits
232
+ - **Philippines (63)**: 10 digits
233
+ - **Thailand (66)**: 9 digits
234
+ - **Malaysia (60)**: 9-10 digits
235
+ - **Singapore (65)**: 8 digits
236
+ - **New Zealand (64)**: 8-10 digits
237
+ - **Vietnam (84)**: 9-10 digits
183
238
  - **Pakistan (92)**: 10 digits
184
- - **And 30+ more countries...**
239
+ - **Turkey (90)**: 10 digits
240
+ - **UAE (971)**: 9 digits
241
+ - **Israel (972)**: 9 digits
242
+ - **Hong Kong (852)**: 8 digits
243
+ - **Taiwan (886)**: 9 digits
244
+ - **And more...**
245
+
246
+ #### Africa & Middle East
247
+
248
+ - **South Africa (27)**: 9 digits
249
+ - **Nigeria (234)**: 10 digits
250
+ - **Kenya (254)**: 9 digits
251
+ - **Egypt (20)**: 8-10 digits
252
+ - **Morocco (212)**: 9 digits
253
+ - **Qatar (974)**: 8 digits
254
+ - **And more...**
185
255
 
186
256
  ### Other Countries
187
- - General validation: 4-15 digits (ITU-T E.164 standard)
257
+
258
+ - **General validation**: 4-15 digits (ITU-T E.164 standard)
259
+ - All countries from CountryCodes.json are supported
260
+ - Falls back to general validation for countries without specific rules
188
261
 
189
262
  ## Examples
190
263
 
191
264
  ```typescript
192
- import { validatePhoneNumber } from 'global-phone-validator';
265
+ import { validatePhoneNumber } from "global-phone-validator";
193
266
 
194
267
  // Valid Indian mobile number
195
- validatePhoneNumber('+91 98765 43210');
268
+ validatePhoneNumber("+91 98765 43210");
196
269
  // { isValid: true, isMobile: true, e164: '+919876543210', ... }
197
270
 
198
271
  // Valid Indian landline
199
- validatePhoneNumber('0123456789', 'IN');
272
+ validatePhoneNumber("0123456789", "IN");
200
273
  // { isValid: true, isFixedLine: true, ... }
201
274
 
202
275
  // Invalid number
203
- validatePhoneNumber('12345');
276
+ validatePhoneNumber("12345");
204
277
  // { isValid: false }
205
278
 
206
279
  // US number
207
- validatePhoneNumber('+1 555 123 4567');
280
+ validatePhoneNumber("+1 555 123 4567");
208
281
  // { isValid: true, country: 'US', e164: '+15551234567', ... }
209
282
 
210
283
  // UK number
211
- validatePhoneNumber('+44 20 7946 0958');
284
+ validatePhoneNumber("+44 20 7946 0958");
212
285
  // { isValid: true, country: 'GB', e164: '+442079460958', ... }
286
+
287
+ // Germany number
288
+ validatePhoneNumber("+49 30 12345678");
289
+ // { isValid: true, country: 'DE', e164: '+493012345678', ... }
290
+
291
+ // France number
292
+ validatePhoneNumber("+33 1 23 45 67 89");
293
+ // { isValid: true, country: 'FR', e164: '+33123456789', ... }
294
+
295
+ // Australia number
296
+ validatePhoneNumber("+61 2 1234 5678");
297
+ // { isValid: true, country: 'AU', e164: '+61212345678', ... }
298
+
299
+ // Brazil number
300
+ validatePhoneNumber("+55 11 98765 4321");
301
+ // { isValid: true, country: 'BR', e164: '+5511987654321', ... }
302
+
303
+ // China number
304
+ validatePhoneNumber("+86 138 0013 8000");
305
+ // { isValid: true, country: 'CN', e164: '+8613800138000', ... }
306
+
307
+ // Japan number
308
+ validatePhoneNumber("+81 3 1234 5678");
309
+ // { isValid: true, country: 'JP', e164: '+81312345678', ... }
213
310
  ```
214
311
 
312
+ ## Why Use This Package?
313
+
314
+ - **Accurate**: Country-specific validation rules ensure phone numbers are validated correctly
315
+ - **Comprehensive**: Supports all countries with specific rules for 50+ major countries
316
+ - **Lightweight**: Zero dependencies, small package size
317
+ - **Type-Safe**: Full TypeScript support with comprehensive type definitions
318
+ - **Flexible**: Handles multiple input formats (international, 0-prefixed, plain digits)
319
+ - **Standardized**: Returns E.164 format for consistent phone number representation
320
+
215
321
  ## Development
216
322
 
217
323
  ```bash
@@ -225,11 +331,29 @@ npm run build
225
331
  npm test
226
332
  ```
227
333
 
334
+ ## Contributing
335
+
336
+ Contributions are welcome! Please feel free to submit a Pull Request.
337
+
338
+ ### Adding Country Validation Rules
339
+
340
+ If you'd like to add or improve validation rules for specific countries, please:
341
+
342
+ 1. Fork the repository
343
+ 2. Add the validation rule to the `validationRules` object in `src/index.ts`
344
+ 3. Update the README with the country information
345
+ 4. Submit a Pull Request
346
+
228
347
  ## License
229
348
 
230
349
  MIT
231
350
 
232
- ## Contributing
351
+ ## Links
233
352
 
234
- Contributions are welcome! Please feel free to submit a Pull Request.
353
+ - **NPM Package**: [global-phone-validator](https://www.npmjs.com/package/global-phone-validator)
354
+ - **GitHub Repository**: [AVantala/global-phone-validator](https://github.com/AVantala/global-phone-validator)
355
+ - **Issues**: [Report a bug or request a feature](https://github.com/AVantala/global-phone-validator/issues)
235
356
 
357
+ ## License
358
+
359
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "global-phone-validator",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "A reusable Node.js + TypeScript package for validating phone numbers worldwide. Supports any country using CountryCodes JSON, handles international formats (+CC), 0-prefixed, and plain digits. Includes mobile/landline detection for India and returns standardized E.164 format.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",