global-phone-validator 1.0.0

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 ADDED
@@ -0,0 +1,218 @@
1
+ # phone-validator
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.
4
+
5
+ ## Features
6
+
7
+ - ✅ **Worldwide Support**: Validates phone numbers for any country using CountryCodes JSON
8
+ - ✅ **Multiple Formats**: Handles international format (+CC), 0-prefixed, and plain digit formats
9
+ - ✅ **India-Specific**: Mobile vs landline detection for Indian phone numbers
10
+ - ✅ **Standardized Output**: Returns E.164 format (+CCNNNNNNNNN)
11
+ - ✅ **TypeScript**: Fully typed with TypeScript definitions
12
+ - ✅ **Zero Dependencies**: No external dependencies required
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install global-phone-validator
18
+ # or
19
+ yarn add global-phone-validator
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ### Basic Validation
25
+
26
+ ```typescript
27
+ import { validatePhoneNumber } from 'global-phone-validator';
28
+
29
+ // International format with country code
30
+ const result1 = validatePhoneNumber('+91 98765 43210');
31
+ console.log(result1);
32
+ // {
33
+ // isValid: true,
34
+ // countryCode: '91',
35
+ // nationalNumber: '9876543210',
36
+ // e164: '+919876543210',
37
+ // isMobile: true,
38
+ // isFixedLine: false,
39
+ // country: 'IN',
40
+ // countryName: 'India'
41
+ // }
42
+
43
+ // 0-prefixed format (India)
44
+ const result2 = validatePhoneNumber('09876543210');
45
+ console.log(result2.isValid); // true
46
+
47
+ // Plain digits with default country
48
+ const result3 = validatePhoneNumber('9876543210', 'IN');
49
+ console.log(result3.isValid); // true
50
+
51
+ // US number
52
+ const result4 = validatePhoneNumber('+1 555 123 4567');
53
+ console.log(result4);
54
+ // {
55
+ // isValid: true,
56
+ // countryCode: '1',
57
+ // nationalNumber: '5551234567',
58
+ // e164: '+15551234567',
59
+ // country: 'US',
60
+ // countryName: 'United States'
61
+ // }
62
+ ```
63
+
64
+ ### Mobile-Only Validation (India)
65
+
66
+ ```typescript
67
+ // Only accept mobile numbers
68
+ const result = validatePhoneNumber('9876543210', 'IN', true);
69
+ console.log(result.isValid); // true (mobile number)
70
+
71
+ const result2 = validatePhoneNumber('0123456789', 'IN', true);
72
+ console.log(result2.isValid); // false (landline number)
73
+ ```
74
+
75
+ ### Get Country Codes
76
+
77
+ ```typescript
78
+ import { getAllCountryCodes, getCountryDialCode, getCountryCodeByDialCode } from 'global-phone-validator';
79
+
80
+ // Get all country codes
81
+ const countries = getAllCountryCodes();
82
+ console.log(countries); // Array of all countries
83
+
84
+ // Get dial code for a country
85
+ const dialCode = getCountryDialCode('IN');
86
+ console.log(dialCode); // '91'
87
+
88
+ // Get country by dial code
89
+ const country = getCountryCodeByDialCode('91');
90
+ console.log(country); // { name: 'India', dial_code: '+91', code: 'IN' }
91
+ ```
92
+
93
+ ## API Reference
94
+
95
+ ### `validatePhoneNumber(input, defaultCountry?, mobileOnly?)`
96
+
97
+ Validates a phone number and returns detailed information.
98
+
99
+ **Parameters:**
100
+ - `input` (string): Phone number in various formats
101
+ - `defaultCountry` (string, optional): ISO country code (e.g., "IN", "US") used when country cannot be detected. Default: "IN"
102
+ - `mobileOnly` (boolean, optional): If true, only accepts mobile numbers (currently only for India). Default: false
103
+
104
+ **Returns:** `PhoneValidationResult`
105
+
106
+ ```typescript
107
+ interface PhoneValidationResult {
108
+ 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'
116
+ }
117
+ ```
118
+
119
+ ### `getAllCountryCodes()`
120
+
121
+ Returns an array of all supported countries.
122
+
123
+ **Returns:** `CountryCode[]`
124
+
125
+ ```typescript
126
+ interface CountryCode {
127
+ name: string; // e.g., 'India'
128
+ dial_code: string; // e.g., '+91'
129
+ code: string; // e.g., 'IN'
130
+ }
131
+ ```
132
+
133
+ ### `getCountryDialCode(countryCode)`
134
+
135
+ Gets the dial code for a country.
136
+
137
+ **Parameters:**
138
+ - `countryCode` (string): ISO country code (e.g., "IN")
139
+
140
+ **Returns:** `string | null` - The dial code without the + sign (e.g., "91")
141
+
142
+ ### `getCountryCodeByDialCode(dialCode)`
143
+
144
+ Gets country information by dial code.
145
+
146
+ **Parameters:**
147
+ - `dialCode` (string): Dial code with or without + (e.g., "91" or "+91")
148
+
149
+ **Returns:** `CountryCode | null`
150
+
151
+ ## Supported Formats
152
+
153
+ The package handles phone numbers in the following formats:
154
+
155
+ 1. **International format**: `+91 98765 43210` or `+919876543210`
156
+ 2. **0-prefixed**: `09876543210` (uses defaultCountry parameter)
157
+ 3. **Plain digits**: `9876543210` (uses defaultCountry parameter)
158
+
159
+ ## Country-Specific Validation
160
+
161
+ ### India (91)
162
+ - Mobile numbers: Must start with 6, 7, 8, or 9 and be exactly 10 digits
163
+ - Landline numbers: Start with 0-5 and be exactly 10 digits
164
+ - Mobile/landline detection is automatically performed
165
+
166
+ ### United States/Canada (1)
167
+ - Must be exactly 10 digits (without country code)
168
+
169
+ ### Other Countries
170
+ - General validation: 4-15 digits
171
+
172
+ ## Examples
173
+
174
+ ```typescript
175
+ import { validatePhoneNumber } from 'global-phone-validator';
176
+
177
+ // Valid Indian mobile number
178
+ validatePhoneNumber('+91 98765 43210');
179
+ // { isValid: true, isMobile: true, e164: '+919876543210', ... }
180
+
181
+ // Valid Indian landline
182
+ validatePhoneNumber('0123456789', 'IN');
183
+ // { isValid: true, isFixedLine: true, ... }
184
+
185
+ // Invalid number
186
+ validatePhoneNumber('12345');
187
+ // { isValid: false }
188
+
189
+ // US number
190
+ validatePhoneNumber('+1 555 123 4567');
191
+ // { isValid: true, country: 'US', e164: '+15551234567', ... }
192
+
193
+ // UK number
194
+ validatePhoneNumber('+44 20 7946 0958');
195
+ // { isValid: true, country: 'GB', e164: '+442079460958', ... }
196
+ ```
197
+
198
+ ## Development
199
+
200
+ ```bash
201
+ # Install dependencies
202
+ npm install
203
+
204
+ # Build
205
+ npm run build
206
+
207
+ # Run tests
208
+ npm test
209
+ ```
210
+
211
+ ## License
212
+
213
+ MIT
214
+
215
+ ## Contributing
216
+
217
+ Contributions are welcome! Please feel free to submit a Pull Request.
218
+