Skip to content

Commit

Permalink
Support mobileOnly argument in isValidNumber. Closes #1535
Browse files Browse the repository at this point in the history
  • Loading branch information
jackocnr committed Mar 7, 2024
1 parent d6552cb commit ab1f731
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 51 deletions.
4 changes: 2 additions & 2 deletions build/js/intlTelInput-jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -1683,9 +1683,9 @@
}
}, {
key: "isValidNumber",
value: function isValidNumber() {
value: function isValidNumber(mobileOnly) {
var val = this._getFullNumber();
return window.intlTelInputUtils ? intlTelInputUtils.isPossibleNumber(val, this.selectedCountryData.iso2) : null;
return window.intlTelInputUtils ? intlTelInputUtils.isPossibleNumber(val, this.selectedCountryData.iso2, mobileOnly) : null;
}
}, {
key: "isValidNumberPrecise",
Expand Down
2 changes: 1 addition & 1 deletion build/js/intlTelInput-jquery.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions build/js/intlTelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -1678,9 +1678,9 @@
}
}, {
key: "isValidNumber",
value: function isValidNumber() {
value: function isValidNumber(mobileOnly) {
var val = this._getFullNumber();
return window.intlTelInputUtils ? intlTelInputUtils.isPossibleNumber(val, this.selectedCountryData.iso2) : null;
return window.intlTelInputUtils ? intlTelInputUtils.isPossibleNumber(val, this.selectedCountryData.iso2, mobileOnly) : null;
}
}, {
key: "isValidNumberPrecise",
Expand Down
2 changes: 1 addition & 1 deletion build/js/intlTelInput.min.js

Large diffs are not rendered by default.

56 changes: 28 additions & 28 deletions build/js/utils.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion react/build/IntlTelInput.cjs.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions react/build/IntlTelInput.cjs.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion react/build/IntlTelInput.esm.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions react/build/IntlTelInput.esm.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions react/demo/simple-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -24867,9 +24867,9 @@ In order to be iterable, non-array objects must have a [Symbol.iterator]() metho
return intlTelInputUtils.getValidationError(this._getFullNumber(), t);
}
return -99;
} }, { key: "isValidNumber", value: function() {
var t = this._getFullNumber();
return window.intlTelInputUtils ? intlTelInputUtils.isPossibleNumber(t, this.selectedCountryData.iso2) : null;
} }, { key: "isValidNumber", value: function(t) {
var e = this._getFullNumber();
return window.intlTelInputUtils ? intlTelInputUtils.isPossibleNumber(e, this.selectedCountryData.iso2, t) : null;
} }, { key: "isValidNumberPrecise", value: function() {
var t = this._getFullNumber();
return window.intlTelInputUtils ? intlTelInputUtils.isValidNumber(t, this.selectedCountryData.iso2) : null;
Expand Down
6 changes: 3 additions & 3 deletions react/demo/validation-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -24867,9 +24867,9 @@ In order to be iterable, non-array objects must have a [Symbol.iterator]() metho
return intlTelInputUtils.getValidationError(this._getFullNumber(), t);
}
return -99;
} }, { key: "isValidNumber", value: function() {
var t = this._getFullNumber();
return window.intlTelInputUtils ? intlTelInputUtils.isPossibleNumber(t, this.selectedCountryData.iso2) : null;
} }, { key: "isValidNumber", value: function(t) {
var e = this._getFullNumber();
return window.intlTelInputUtils ? intlTelInputUtils.isPossibleNumber(e, this.selectedCountryData.iso2, t) : null;
} }, { key: "isValidNumberPrecise", value: function() {
var t = this._getFullNumber();
return window.intlTelInputUtils ? intlTelInputUtils.isValidNumber(t, this.selectedCountryData.iso2) : null;
Expand Down
4 changes: 2 additions & 2 deletions src/js/intlTelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -1860,10 +1860,10 @@ class Iti {
}

// validate the input val - assumes the global function isPossibleNumber (from utilsScript)
isValidNumber() {
isValidNumber(mobileOnly) {
const val = this._getFullNumber();
return window.intlTelInputUtils
? intlTelInputUtils.isPossibleNumber(val, this.selectedCountryData.iso2)
? intlTelInputUtils.isPossibleNumber(val, this.selectedCountryData.iso2, mobileOnly)
: null;
}

Expand Down
9 changes: 8 additions & 1 deletion src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,17 @@ const isValidNumber = (number, countryCode) => {
};

// check if given number is possible
const isPossibleNumber = (number, countryCode) => {
const isPossibleNumber = (number, countryCode, mobileOnly) => {
try {
const phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
const numberObj = phoneUtil.parseAndKeepRawInput(number, countryCode);

if (mobileOnly) {
const resultMobile = phoneUtil.isPossibleNumberForTypeWithReason(numberObj, i18n.phonenumbers.PhoneNumberType.MOBILE);
const isPossibleMobile = resultMobile === i18n.phonenumbers.PhoneNumberUtil.ValidationResult.IS_POSSIBLE;
return isPossibleMobile;
}

// can't use phoneUtil.isPossibleNumber directly as it accepts IS_POSSIBLE_LOCAL_ONLY numbers e.g. local numbers that are much shorter
const result = phoneUtil.isPossibleNumberWithReason(numberObj);
const isPossible = result === i18n.phonenumbers.PhoneNumberUtil.ValidationResult.IS_POSSIBLE;
Expand Down

0 comments on commit ab1f731

Please sign in to comment.