Skip to content

Commit

Permalink
Fix validationNumberType=FIXED_LINE_OR_MOBILE
Browse files Browse the repository at this point in the history
  • Loading branch information
jackocnr committed Jul 20, 2024
1 parent 18a25e7 commit bb34f63
Show file tree
Hide file tree
Showing 9 changed files with 1,610 additions and 1,550 deletions.
506 changes: 257 additions & 249 deletions build/js/intlTelInputWithUtils.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/js/intlTelInputWithUtils.min.js

Large diffs are not rendered by default.

104 changes: 52 additions & 52 deletions build/js/utils.js

Large diffs are not rendered by default.

506 changes: 257 additions & 249 deletions react/build/IntlTelInputWithUtils.cjs

Large diffs are not rendered by default.

506 changes: 257 additions & 249 deletions react/build/IntlTelInputWithUtils.js

Large diffs are not rendered by default.

506 changes: 257 additions & 249 deletions react/demo/set-number-bundle.js

Large diffs are not rendered by default.

506 changes: 257 additions & 249 deletions react/demo/simple-bundle.js

Large diffs are not rendered by default.

506 changes: 257 additions & 249 deletions react/demo/validation-bundle.js

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,28 @@ const isValidNumber = (number, countryCode) => {
}
};

//* For internal use only - see isPossibleNumber.
const isPossibleNumberForType = (phoneUtil, numberObj, numberTypeName) => {
const resultForType = phoneUtil.isPossibleNumberForTypeWithReason(numberObj, numberType[numberTypeName]);
const isPossibleForType = resultForType === i18n.phonenumbers.PhoneNumberUtil.ValidationResult.IS_POSSIBLE;
return isPossibleForType;
};

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

if (numberTypeName) {
const resultForType = phoneUtil.isPossibleNumberForTypeWithReason(numberObj, numberType[numberTypeName]);
const isPossibleForType = resultForType === i18n.phonenumbers.PhoneNumberUtil.ValidationResult.IS_POSSIBLE;
return isPossibleForType;
const isPossible = isPossibleNumberForType(phoneUtil, numberObj, numberTypeName);
//* FIXED_LINE_OR_MOBILE does not behave how you would expect - it is its own category that is different to either MOBILE or FIXED_LINE (e.g. for US numbers which could be used for either purpose - it should really be called something like FIXED_LINE_SLASH_MOBILE). So here we make it more user friendly by checking if it's a possible number for any of those three categories. NOTE: this is actually in-line with how it behaves in other situations e.g. if you call isPossibleNumberForType with type="MOBILE" and the number set to a US number, it returns VALID even though it's type is technically FIXED_LINE_OR_MOBILE.
if (numberTypeName === "FIXED_LINE_OR_MOBILE") {
const isMobile = isPossibleNumberForType(phoneUtil, numberObj, "MOBILE");
const isFixedLine = isPossibleNumberForType(phoneUtil, numberObj, "FIXED_LINE");
return isMobile || isFixedLine || isPossible;
}
return isPossible;
}

//* Can't use phoneUtil.isPossibleNumber directly as it accepts IS_POSSIBLE_LOCAL_ONLY numbers e.g. local numbers that are much shorter.
Expand Down

0 comments on commit bb34f63

Please sign in to comment.