intl-tel-input 18.1.7 → 18.2.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.
@@ -154,6 +154,9 @@ class Iti {
154
154
  }
155
155
  }
156
156
 
157
+ // check if input has one parent with RTL
158
+ this.isRTL = !!this.telInput.closest('[dir=rtl]');
159
+
157
160
  // these promises get resolved when their individual requests complete
158
161
  // this way the dev can do something like iti.promise.then(...) to know when all requests are
159
162
  // complete
@@ -1202,7 +1205,11 @@ class Iti {
1202
1205
  this.selectedFlag.offsetWidth || this._getHiddenSelectedFlagWidth();
1203
1206
 
1204
1207
  // add 6px of padding after the grey selected-dial-code box, as this is what we use in the css
1205
- this.telInput.style.paddingLeft = `${selectedFlagWidth + 6}px`;
1208
+ if (this.isRTL) {
1209
+ this.telInput.style.paddingRight = `${selectedFlagWidth + 6}px`;
1210
+ } else {
1211
+ this.telInput.style.paddingLeft = `${selectedFlagWidth + 6}px`;
1212
+ }
1206
1213
  }
1207
1214
 
1208
1215
  // and the input's placeholder
@@ -1650,6 +1657,14 @@ class Iti {
1650
1657
  : null;
1651
1658
  }
1652
1659
 
1660
+ // check if input val is possible number (weaker validation, but more future-proof) - assumes the global function isPossibleNumber (from utilsScript)
1661
+ isPossibleNumber() {
1662
+ const val = this._getFullNumber().trim();
1663
+ return window.intlTelInputUtils
1664
+ ? intlTelInputUtils.isPossibleNumber(val, this.selectedCountryData.iso2)
1665
+ : null;
1666
+ }
1667
+
1653
1668
  // update the selected flag, and update the input val accordingly
1654
1669
  setCountry(originalCountryCode) {
1655
1670
  const countryCode = originalCountryCode.toLowerCase();
package/src/js/utils.js CHANGED
@@ -102,6 +102,19 @@ const isValidNumber = (number, countryCode) => {
102
102
  }
103
103
  };
104
104
 
105
+ // check if given number is possible
106
+ const isPossibleNumber = (number, countryCode) => {
107
+ try {
108
+ const phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
109
+ const numberObj = phoneUtil.parseAndKeepRawInput(number, countryCode);
110
+ // can't use phoneUtil.isPossibleNumber directly as it accepts IS_POSSIBLE_LOCAL_ONLY numbers e.g. local numbers that are much shorter
111
+ const result = phoneUtil.isPossibleNumberWithReason(numberObj);
112
+ return result === i18n.phonenumbers.PhoneNumberUtil.ValidationResult.IS_POSSIBLE;
113
+ } catch (e) {
114
+ return false;
115
+ }
116
+ };
117
+
105
118
  /********************
106
119
  * NOTE: for following sections, keys must be in quotes to force closure compiler to preserve them
107
120
  ********************/
@@ -149,6 +162,7 @@ goog.exportSymbol("intlTelInputUtils.getExtension", getExtension);
149
162
  goog.exportSymbol("intlTelInputUtils.getNumberType", getNumberType);
150
163
  goog.exportSymbol("intlTelInputUtils.getValidationError", getValidationError);
151
164
  goog.exportSymbol("intlTelInputUtils.isValidNumber", isValidNumber);
165
+ goog.exportSymbol("intlTelInputUtils.isPossibleNumber", isPossibleNumber);
152
166
  // enums
153
167
  goog.exportSymbol("intlTelInputUtils.numberFormat", numberFormat);
154
168
  goog.exportSymbol("intlTelInputUtils.numberType", numberType);
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+
3
+ describe("isPossibleNumber:", function() {
4
+
5
+ beforeEach(function() {
6
+ intlSetup(true);
7
+ input = $("<input>").wrap("div");
8
+ });
9
+
10
+ afterEach(function() {
11
+ intlTeardown();
12
+ });
13
+
14
+
15
+
16
+ describe("init plugin and call public method isPossibleNumber", function() {
17
+
18
+ beforeEach(function() {
19
+ iti = window.intlTelInput(input[0]);
20
+ });
21
+
22
+ it("returns true for: valid intl number", function() {
23
+ iti.setNumber("+44 7733 123456");
24
+ expect(iti.isPossibleNumber()).toBeTruthy();
25
+ });
26
+
27
+ it("returns true for: possible but invalid (bad dial code) intl number", function() {
28
+ iti.setNumber("+44 9999 123456");
29
+ expect(iti.isPossibleNumber()).toBeTruthy();
30
+ });
31
+
32
+ it("returns false for: invalid (too short by 2 digits) intl number", function() {
33
+ iti.setNumber("+44 7733 1234");
34
+ expect(iti.isPossibleNumber()).toBeFalsy();
35
+ });
36
+
37
+ // guess this is a quirk of UK phone numbers that some valid ones are only 10 digits (e.g. 0773312345)
38
+ it("returns true for: invalid (too short by 1 digit) intl number", function() {
39
+ iti.setNumber("+44 7733 12345");
40
+ expect(iti.isPossibleNumber()).toBeTruthy();
41
+ });
42
+
43
+ it("returns false for: invalid (too long) intl number", function() {
44
+ iti.setNumber("+44 7733 1234567");
45
+ expect(iti.isPossibleNumber()).toBeFalsy();
46
+ });
47
+
48
+ it("returns null when utils script is not available", function() {
49
+ delete window.intlTelInputUtils;
50
+ iti.setNumber("+44 7733 123456");
51
+ expect(iti.isPossibleNumber()).toBeNull();
52
+ });
53
+
54
+ });
55
+
56
+ });
@@ -24,22 +24,22 @@ describe("isValidNumber:", function() {
24
24
  expect(iti.isValidNumber()).toBeTruthy();
25
25
  });
26
26
 
27
- it("returns false for: invalid intl number", function() {
27
+ it("returns false for: invalid (too short) intl number", function() {
28
28
  iti.setNumber("+44 7733 123");
29
29
  expect(iti.isValidNumber()).toBeFalsy();
30
30
  });
31
31
 
32
+ it("returns false for: possible but invalid (bad dial code) intl number", function() {
33
+ iti.setNumber("+44 9999 123456");
34
+ expect(iti.isValidNumber()).toBeFalsy();
35
+ });
36
+
32
37
  it("returns null when utils script is not available", function() {
33
38
  delete window.intlTelInputUtils;
34
39
  iti.setNumber("+44 7733 123456");
35
40
  expect(iti.isValidNumber()).toBeNull();
36
41
  });
37
42
 
38
- /*it("returns false for: valid intl number containing alpha chars", function() {
39
- iti.setNumber("+44 7733 123 abc");
40
- expect(iti.isValidNumber()).toBeFalsy();
41
- });*/
42
-
43
43
  });
44
44
 
45
45
 
@@ -62,7 +62,7 @@ describe("isValidNumber:", function() {
62
62
  expect(iti.isValidNumber()).toBeTruthy();
63
63
  });
64
64
 
65
- it("returns false for: correct selected country, invalid number", function() {
65
+ it("returns false for: correct selected country, invalid (too short) number", function() {
66
66
  iti.setCountry("gb");
67
67
  iti.setNumber("07733 123");
68
68
  expect(iti.isValidNumber()).toBeFalsy();