首页 > 解决方案 > 处理手机号码中的连续号码

问题描述

在我的 react-native 应用程序中,我想验证手机号码。所以我使用了 google-libphonenumber 库。

现在有一个要求,避免手机号码中的连续号码。例如:在新加坡,没有国家代码的手机号码有 8 位数字。在这里,我们不应该有 7 个相同数字的连续数字。

+65 81111111

这应该是错误的。

如何使用 google-libphonenumber 库验证这一点?这是我的验证码。

/** mobile with country code */
const mobileWithCode = (value: any) => {
  const regex = /^[+](\d){8,13}$/;

  if (regex.test(value)) {
    try {
      const phoneNumberUtil = PhoneNumberUtil.getInstance();
      const mobile = phoneNumberUtil.parseAndKeepRawInput(value, '');
      const phoneType = phoneNumberUtil.getNumberType(mobile);

      if (phoneType === PhoneNumberType.MOBILE) {
        console.log(PhoneNumberType.TOLL_FREE);
        return phoneNumberUtil.isValidNumber(mobile);
      }
      return false;
    } catch (err) {
      return false;
    }
  }

  return false;
};

标签: javascriptreact-native

解决方案


推荐阅读