@jacksonavila/phone-lib 2.0.7 → 2.0.9

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
@@ -13,6 +13,7 @@ Librería JavaScript moderna para integrar fácilmente un input de teléfono con
13
13
 
14
14
  - ✅ **Country dropdown** / **Dropdown de países** showing name, ISO2 code, dial code, and flag
15
15
  - ✅ **Tel input** / **Input tipo tel** with automatic formatting based on selected country
16
+ - ✅ **Numbers only input** / **Input solo números** - automatically filters non-numeric characters
16
17
  - ✅ **Phone number validation** / **Validación de números** using `libphonenumber-js`
17
18
  - ✅ **Complete public API** / **API pública completa** with methods to get number information
18
19
  - ✅ **Vanilla JavaScript and React support** / **Soporte para Vanilla JavaScript y React**
@@ -61,7 +62,7 @@ You can use PhoneLib directly from CDN without npm / Puedes usar PhoneLib direct
61
62
  <!DOCTYPE html>
62
63
  <html>
63
64
  <head>
64
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.7/phone-lib.css">
65
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.9/phone-lib.css">
65
66
  </head>
66
67
  <body>
67
68
  <div id="phone-container"></div>
@@ -69,7 +70,7 @@ You can use PhoneLib directly from CDN without npm / Puedes usar PhoneLib direct
69
70
  <script type="importmap">
70
71
  {
71
72
  "imports": {
72
- "@jacksonavila/phone-lib": "https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.7/phone-lib.js",
73
+ "@jacksonavila/phone-lib": "https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.9/phone-lib.js",
73
74
  "libphonenumber-js": "https://esm.sh/libphonenumber-js@1.11.0"
74
75
  }
75
76
  }
@@ -89,8 +90,8 @@ You can use PhoneLib directly from CDN without npm / Puedes usar PhoneLib direct
89
90
  ```
90
91
 
91
92
  **CDN URLs / URLs de CDN:**
92
- - **jsDelivr:** `https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.7/`
93
- - **unpkg:** `https://unpkg.com/@jacksonavila/phone-lib@2.0.7/`
93
+ - **jsDelivr:** `https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.9/`
94
+ - **unpkg:** `https://unpkg.com/@jacksonavila/phone-lib@2.0.9/`
94
95
 
95
96
  ### Method 2: Script Tag (All Browsers) / Método 2: Script Tag (Todos los Navegadores)
96
97
 
@@ -98,12 +99,12 @@ You can use PhoneLib directly from CDN without npm / Puedes usar PhoneLib direct
98
99
  <!DOCTYPE html>
99
100
  <html>
100
101
  <head>
101
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.7/phone-lib.css">
102
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.9/phone-lib.css">
102
103
  </head>
103
104
  <body>
104
105
  <div id="phone-container"></div>
105
106
 
106
- <script src="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.7/phone-lib.cdn.js"></script>
107
+ <script src="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.9/phone-lib.cdn.js"></script>
107
108
 
108
109
  <script>
109
110
  let phoneLib = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jacksonavila/phone-lib",
3
- "version": "2.0.7",
3
+ "version": "2.0.9",
4
4
  "description": "Librería JavaScript para input de teléfono con selector de país y banderas - Compatible con Vanilla JS y React",
5
5
  "main": "phone-lib.js",
6
6
  "module": "phone-lib.js",
package/phone-lib.cdn.js CHANGED
@@ -4,8 +4,8 @@
4
4
  * Carga libphonenumber-js dinámicamente y expone PhoneLib globalmente
5
5
  *
6
6
  * Uso:
7
- * <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.7/phone-lib.css">
8
- * <script src="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.7/phone-lib.cdn.js"></script>
7
+ * <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.9/phone-lib.css">
8
+ * <script src="https://cdn.jsdelivr.net/npm/@jacksonavila/phone-lib@2.0.9/phone-lib.cdn.js"></script>
9
9
  * <script>
10
10
  * document.addEventListener('phoneLibReady', () => {
11
11
  * const phoneLib = new PhoneLib('#container', {...});
@@ -17,7 +17,7 @@
17
17
  'use strict';
18
18
 
19
19
  // Versión del paquete (actualizar cuando se publique nueva versión)
20
- const PACKAGE_VERSION = '2.0.7';
20
+ const PACKAGE_VERSION = '2.0.9';
21
21
  const PACKAGE_NAME = '@jacksonavila/phone-lib';
22
22
 
23
23
  // URLs de CDN
package/phone-lib.js CHANGED
@@ -254,68 +254,256 @@ class PhoneLib {
254
254
  */
255
255
  getCountryName(iso2) {
256
256
  const countryNames = {
257
- 'US': 'Estados Unidos',
258
- 'CO': 'Colombia',
259
- 'ES': 'España',
260
- 'MX': 'México',
257
+ 'AD': 'Andorra',
258
+ 'AE': 'Emiratos Árabes Unidos',
259
+ 'AF': 'Afganistán',
260
+ 'AG': 'Antigua y Barbuda',
261
+ 'AI': 'Anguila',
262
+ 'AL': 'Albania',
263
+ 'AM': 'Armenia',
264
+ 'AO': 'Angola',
265
+ 'AQ': 'Antártida',
261
266
  'AR': 'Argentina',
262
- 'CL': 'Chile',
263
- 'PE': 'Perú',
264
- 'VE': 'Venezuela',
265
- 'EC': 'Ecuador',
267
+ 'AS': 'Samoa Americana',
268
+ 'AT': 'Austria',
269
+ 'AU': 'Australia',
270
+ 'AW': 'Aruba',
271
+ 'AX': 'Islas Åland',
272
+ 'AZ': 'Azerbaiyán',
273
+ 'BA': 'Bosnia y Herzegovina',
274
+ 'BB': 'Barbados',
275
+ 'BD': 'Bangladesh',
276
+ 'BE': 'Bélgica',
277
+ 'BF': 'Burkina Faso',
278
+ 'BG': 'Bulgaria',
279
+ 'BH': 'Baréin',
280
+ 'BI': 'Burundi',
281
+ 'BJ': 'Benín',
282
+ 'BL': 'San Bartolomé',
283
+ 'BM': 'Bermudas',
284
+ 'BN': 'Brunéi',
266
285
  'BO': 'Bolivia',
267
- 'PY': 'Paraguay',
268
- 'UY': 'Uruguay',
269
- 'CR': 'Costa Rica',
270
- 'PA': 'Panamá',
271
- 'GT': 'Guatemala',
272
- 'HN': 'Honduras',
273
- 'NI': 'Nicaragua',
274
- 'SV': 'El Salvador',
275
- 'DO': 'República Dominicana',
276
- 'CU': 'Cuba',
277
- 'PR': 'Puerto Rico',
286
+ 'BQ': 'Caribe Neerlandés',
278
287
  'BR': 'Brasil',
288
+ 'BS': 'Bahamas',
289
+ 'BT': 'Bután',
290
+ 'BV': 'Isla Bouvet',
291
+ 'BW': 'Botsuana',
292
+ 'BY': 'Bielorrusia',
293
+ 'BZ': 'Belice',
279
294
  'CA': 'Canadá',
280
- 'GB': 'Reino Unido',
281
- 'FR': 'Francia',
282
- 'DE': 'Alemania',
283
- 'IT': 'Italia',
284
- 'PT': 'Portugal',
285
- 'NL': 'Países Bajos',
286
- 'BE': 'Bélgica',
295
+ 'CC': 'Islas Cocos',
296
+ 'CD': 'República Democrática del Congo',
297
+ 'CF': 'República Centroafricana',
298
+ 'CG': 'Congo',
287
299
  'CH': 'Suiza',
288
- 'AT': 'Austria',
289
- 'SE': 'Suecia',
290
- 'NO': 'Noruega',
300
+ 'CI': 'Costa de Marfil',
301
+ 'CK': 'Islas Cook',
302
+ 'CL': 'Chile',
303
+ 'CM': 'Camerún',
304
+ 'CN': 'China',
305
+ 'CO': 'Colombia',
306
+ 'CR': 'Costa Rica',
307
+ 'CU': 'Cuba',
308
+ 'CV': 'Cabo Verde',
309
+ 'CW': 'Curazao',
310
+ 'CX': 'Isla de Navidad',
311
+ 'CY': 'Chipre',
312
+ 'CZ': 'República Checa',
313
+ 'DE': 'Alemania',
314
+ 'DJ': 'Yibuti',
291
315
  'DK': 'Dinamarca',
316
+ 'DM': 'Dominica',
317
+ 'DO': 'República Dominicana',
318
+ 'DZ': 'Argelia',
319
+ 'EC': 'Ecuador',
320
+ 'EE': 'Estonia',
321
+ 'EG': 'Egipto',
322
+ 'EH': 'Sahara Occidental',
323
+ 'ER': 'Eritrea',
324
+ 'ES': 'España',
325
+ 'ET': 'Etiopía',
292
326
  'FI': 'Finlandia',
293
- 'PL': 'Polonia',
294
- 'CZ': 'República Checa',
327
+ 'FJ': 'Fiyi',
328
+ 'FK': 'Islas Malvinas',
329
+ 'FM': 'Micronesia',
330
+ 'FO': 'Islas Feroe',
331
+ 'FR': 'Francia',
332
+ 'GA': 'Gabón',
333
+ 'GB': 'Reino Unido',
334
+ 'GD': 'Granada',
335
+ 'GE': 'Georgia',
336
+ 'GF': 'Guayana Francesa',
337
+ 'GG': 'Guernsey',
338
+ 'GH': 'Ghana',
339
+ 'GI': 'Gibraltar',
340
+ 'GL': 'Groenlandia',
341
+ 'GM': 'Gambia',
342
+ 'GN': 'Guinea',
343
+ 'GP': 'Guadalupe',
344
+ 'GQ': 'Guinea Ecuatorial',
295
345
  'GR': 'Grecia',
346
+ 'GS': 'Georgia del Sur e Islas Sandwich del Sur',
347
+ 'GT': 'Guatemala',
348
+ 'GU': 'Guam',
349
+ 'GW': 'Guinea-Bisáu',
350
+ 'GY': 'Guyana',
351
+ 'HK': 'Hong Kong',
352
+ 'HM': 'Islas Heard y McDonald',
353
+ 'HN': 'Honduras',
354
+ 'HR': 'Croacia',
355
+ 'HT': 'Haití',
356
+ 'HU': 'Hungría',
357
+ 'ID': 'Indonesia',
296
358
  'IE': 'Irlanda',
297
- 'NZ': 'Nueva Zelanda',
298
- 'AU': 'Australia',
299
- 'JP': 'Japón',
300
- 'CN': 'China',
359
+ 'IL': 'Israel',
360
+ 'IM': 'Isla de Man',
301
361
  'IN': 'India',
362
+ 'IO': 'Territorio Británico del Océano Índico',
363
+ 'IQ': 'Irak',
364
+ 'IR': 'Irán',
365
+ 'IS': 'Islandia',
366
+ 'IT': 'Italia',
367
+ 'JE': 'Jersey',
368
+ 'JM': 'Jamaica',
369
+ 'JO': 'Jordania',
370
+ 'JP': 'Japón',
371
+ 'KE': 'Kenia',
372
+ 'KG': 'Kirguistán',
373
+ 'KH': 'Camboya',
374
+ 'KI': 'Kiribati',
375
+ 'KM': 'Comoras',
376
+ 'KN': 'San Cristóbal y Nieves',
377
+ 'KP': 'Corea del Norte',
302
378
  'KR': 'Corea del Sur',
303
- 'SG': 'Singapur',
379
+ 'KW': 'Kuwait',
380
+ 'KY': 'Islas Caimán',
381
+ 'KZ': 'Kazajistán',
382
+ 'LA': 'Laos',
383
+ 'LB': 'Líbano',
384
+ 'LC': 'Santa Lucía',
385
+ 'LI': 'Liechtenstein',
386
+ 'LK': 'Sri Lanka',
387
+ 'LR': 'Liberia',
388
+ 'LS': 'Lesoto',
389
+ 'LT': 'Lituania',
390
+ 'LU': 'Luxemburgo',
391
+ 'LV': 'Letonia',
392
+ 'LY': 'Libia',
393
+ 'MA': 'Marruecos',
394
+ 'MC': 'Mónaco',
395
+ 'MD': 'Moldavia',
396
+ 'ME': 'Montenegro',
397
+ 'MF': 'San Martín',
398
+ 'MG': 'Madagascar',
399
+ 'MH': 'Islas Marshall',
400
+ 'MK': 'Macedonia del Norte',
401
+ 'ML': 'Malí',
402
+ 'MM': 'Myanmar',
403
+ 'MN': 'Mongolia',
404
+ 'MO': 'Macao',
405
+ 'MP': 'Islas Marianas del Norte',
406
+ 'MQ': 'Martinica',
407
+ 'MR': 'Mauritania',
408
+ 'MS': 'Montserrat',
409
+ 'MT': 'Malta',
410
+ 'MU': 'Mauricio',
411
+ 'MV': 'Maldivas',
412
+ 'MW': 'Malaui',
413
+ 'MX': 'México',
304
414
  'MY': 'Malasia',
305
- 'TH': 'Tailandia',
415
+ 'MZ': 'Mozambique',
416
+ 'NA': 'Namibia',
417
+ 'NC': 'Nueva Caledonia',
418
+ 'NE': 'Níger',
419
+ 'NF': 'Isla Norfolk',
420
+ 'NG': 'Nigeria',
421
+ 'NI': 'Nicaragua',
422
+ 'NL': 'Países Bajos',
423
+ 'NO': 'Noruega',
424
+ 'NP': 'Nepal',
425
+ 'NR': 'Nauru',
426
+ 'NU': 'Niue',
427
+ 'NZ': 'Nueva Zelanda',
428
+ 'OM': 'Omán',
429
+ 'PA': 'Panamá',
430
+ 'PE': 'Perú',
431
+ 'PF': 'Polinesia Francesa',
432
+ 'PG': 'Papúa Nueva Guinea',
306
433
  'PH': 'Filipinas',
307
- 'ID': 'Indonesia',
308
- 'VN': 'Vietnam',
309
- 'AE': 'Emiratos Árabes Unidos',
434
+ 'PK': 'Pakistán',
435
+ 'PL': 'Polonia',
436
+ 'PM': 'San Pedro y Miquelón',
437
+ 'PN': 'Islas Pitcairn',
438
+ 'PR': 'Puerto Rico',
439
+ 'PS': 'Palestina',
440
+ 'PT': 'Portugal',
441
+ 'PW': 'Palaos',
442
+ 'PY': 'Paraguay',
443
+ 'QA': 'Catar',
444
+ 'RE': 'Reunión',
445
+ 'RO': 'Rumania',
446
+ 'RS': 'Serbia',
447
+ 'RU': 'Rusia',
448
+ 'RW': 'Ruanda',
310
449
  'SA': 'Arabia Saudí',
311
- 'IL': 'Israel',
450
+ 'SB': 'Islas Salomón',
451
+ 'SC': 'Seychelles',
452
+ 'SD': 'Sudán',
453
+ 'SE': 'Suecia',
454
+ 'SG': 'Singapur',
455
+ 'SH': 'Santa Elena',
456
+ 'SI': 'Eslovenia',
457
+ 'SJ': 'Svalbard y Jan Mayen',
458
+ 'SK': 'Eslovaquia',
459
+ 'SL': 'Sierra Leona',
460
+ 'SM': 'San Marino',
461
+ 'SN': 'Senegal',
462
+ 'SO': 'Somalia',
463
+ 'SR': 'Surinam',
464
+ 'SS': 'Sudán del Sur',
465
+ 'ST': 'Santo Tomé y Príncipe',
466
+ 'SV': 'El Salvador',
467
+ 'SX': 'Sint Maarten',
468
+ 'SY': 'Siria',
469
+ 'SZ': 'Suazilandia',
470
+ 'TC': 'Islas Turcas y Caicos',
471
+ 'TD': 'Chad',
472
+ 'TF': 'Territorios Australes Franceses',
473
+ 'TG': 'Togo',
474
+ 'TH': 'Tailandia',
475
+ 'TJ': 'Tayikistán',
476
+ 'TK': 'Tokelau',
477
+ 'TL': 'Timor Oriental',
478
+ 'TM': 'Turkmenistán',
479
+ 'TN': 'Túnez',
480
+ 'TO': 'Tonga',
312
481
  'TR': 'Turquía',
313
- 'RU': 'Rusia',
482
+ 'TT': 'Trinidad y Tobago',
483
+ 'TV': 'Tuvalu',
484
+ 'TW': 'Taiwán',
485
+ 'TZ': 'Tanzania',
486
+ 'UA': 'Ucrania',
487
+ 'UG': 'Uganda',
488
+ 'UM': 'Islas Ultramarinas de Estados Unidos',
489
+ 'US': 'Estados Unidos',
490
+ 'UY': 'Uruguay',
491
+ 'UZ': 'Uzbekistán',
492
+ 'VA': 'Ciudad del Vaticano',
493
+ 'VC': 'San Vicente y las Granadinas',
494
+ 'VE': 'Venezuela',
495
+ 'VG': 'Islas Vírgenes Británicas',
496
+ 'VI': 'Islas Vírgenes de los Estados Unidos',
497
+ 'VN': 'Vietnam',
498
+ 'VU': 'Vanuatu',
499
+ 'WF': 'Wallis y Futuna',
500
+ 'WS': 'Samoa',
501
+ 'XK': 'Kosovo',
502
+ 'YE': 'Yemen',
503
+ 'YT': 'Mayotte',
314
504
  'ZA': 'Sudáfrica',
315
- 'EG': 'Egipto',
316
- 'NG': 'Nigeria',
317
- 'KE': 'Kenia',
318
- 'GH': 'Ghana'
505
+ 'ZM': 'Zambia',
506
+ 'ZW': 'Zimbabue'
319
507
  };
320
508
 
321
509
  return countryNames[iso2] || iso2;
@@ -707,11 +895,65 @@ class PhoneLib {
707
895
 
708
896
  // Input de teléfono
709
897
  if (this.phoneInput) {
898
+ // Handler para prevenir entrada de caracteres no numéricos
899
+ this._boundHandlers.phoneKeyDown = (e) => {
900
+ if (this.isDisabled || this.isReadonly) return;
901
+
902
+ // Permitir teclas especiales (Backspace, Delete, Arrow keys, Tab, etc.)
903
+ const allowedKeys = [
904
+ 'Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown',
905
+ 'Home', 'End', 'Tab', 'Escape', 'Enter'
906
+ ];
907
+
908
+ // Permitir Ctrl+A, Ctrl+C, Ctrl+V, Ctrl+X
909
+ if (e.ctrlKey || e.metaKey) {
910
+ if (['a', 'c', 'v', 'x'].includes(e.key.toLowerCase())) {
911
+ return; // Permitir
912
+ }
913
+ }
914
+
915
+ // Si es una tecla permitida, dejar pasar
916
+ if (allowedKeys.includes(e.key)) {
917
+ return;
918
+ }
919
+
920
+ // Permitir números (0-9)
921
+ if (e.key >= '0' && e.key <= '9') {
922
+ return;
923
+ }
924
+
925
+ // Permitir + solo al inicio del input
926
+ if (e.key === '+') {
927
+ const currentValue = e.target.value;
928
+ const selectionStart = e.target.selectionStart;
929
+ // Solo permitir + si está al inicio o si no hay ningún carácter antes de la posición del cursor
930
+ if (selectionStart === 0 || currentValue.substring(0, selectionStart).trim() === '') {
931
+ return;
932
+ }
933
+ }
934
+
935
+ // Si llegamos aquí, prevenir la entrada
936
+ e.preventDefault();
937
+ };
938
+
710
939
  this._boundHandlers.phoneInput = (e) => {
711
940
  if (this.isDisabled || this.isReadonly) return;
712
941
 
713
942
  // Obtener el valor actual del input
714
- const inputValue = e.target.value;
943
+ let inputValue = e.target.value;
944
+
945
+ // Filtrar caracteres no numéricos (permitir números y +)
946
+ // Remover cualquier carácter que no sea número o +
947
+ inputValue = inputValue.replace(/[^0-9+]/g, '');
948
+
949
+ // Si el valor cambió después de filtrar, actualizar el input
950
+ if (inputValue !== e.target.value) {
951
+ const cursorPosition = e.target.selectionStart;
952
+ e.target.value = inputValue;
953
+ // Restaurar posición del cursor (ajustada por caracteres removidos)
954
+ const newPosition = Math.min(cursorPosition, inputValue.length);
955
+ e.target.setSelectionRange(newPosition, newPosition);
956
+ }
715
957
 
716
958
  // Procesar el input
717
959
  this.handlePhoneInput(inputValue);
@@ -733,6 +975,7 @@ class PhoneLib {
733
975
  this.emitEvent('blur', { phoneNumber: this.phoneNumber, isValid });
734
976
  };
735
977
 
978
+ this.phoneInput.addEventListener('keydown', this._boundHandlers.phoneKeyDown);
736
979
  this.phoneInput.addEventListener('input', this._boundHandlers.phoneInput);
737
980
  this.phoneInput.addEventListener('focus', this._boundHandlers.phoneFocus);
738
981
  this.phoneInput.addEventListener('blur', this._boundHandlers.phoneBlur);
@@ -766,6 +1009,9 @@ class PhoneLib {
766
1009
 
767
1010
  // Remover listeners del input
768
1011
  if (this.phoneInput && this._boundHandlers) {
1012
+ if (this._boundHandlers.phoneKeyDown) {
1013
+ this.phoneInput.removeEventListener('keydown', this._boundHandlers.phoneKeyDown);
1014
+ }
769
1015
  if (this._boundHandlers.phoneInput) {
770
1016
  this.phoneInput.removeEventListener('input', this._boundHandlers.phoneInput);
771
1017
  }
@@ -1313,9 +1559,12 @@ class PhoneLib {
1313
1559
  return;
1314
1560
  }
1315
1561
 
1316
- this.phoneNumber = number;
1562
+ // Filtrar caracteres no numéricos (permitir números y +)
1563
+ const filteredNumber = typeof number === 'string' ? number.replace(/[^0-9+]/g, '') : (number || '').toString().replace(/[^0-9+]/g, '');
1564
+
1565
+ this.phoneNumber = filteredNumber;
1317
1566
  if (this.phoneInput) {
1318
- this.phoneInput.value = number;
1567
+ this.phoneInput.value = filteredNumber;
1319
1568
  }
1320
1569
  this.updatePhoneNumber();
1321
1570