@unikue/ts-lang-utils
    Preparing search index...

    Function isMobile

    • Returns whether the given text is a valid mobile phone number

      The alphaOrPattern argument accepts two kinds of values:

      • a two-letter ISO country code (case-insensitive, e.g. CN, US), in which case the number is validated through libphonenumber-js against that country;
      • a regex or regex pattern string, in which case the behaviour is unchanged: the text is tested against that regex (defaulting to the Chinese mobile number rule).

      Parameters

      • Optionaltext: string | null

        The text to check

      • alphaOrPattern: string | RegExp = CHINESE_MOBILE_REGEX

        The ISO alpha-2 country code to validate via libphonenumber-js, or a regex / regex pattern for mobile numbers (defaults to China)

      Returns boolean

      whether the given text is a valid mobile phone number

      David Hsing

      isMobile(undefined);                                    // false
      isMobile('13812345678'); // true
      isMobile('+8613812345678'); // true, 带国家码
      isMobile('+86-13812345678'); // true, 带国家码与连字符
      isMobile('12345678901'); // false
      isMobile('0086-13812345678'); // false, 国际拨号前缀
      isMobile('2125551234', 'US'); // true, 美国(libphonenumber-js 校验)
      isMobile('2125551234', '^[2-9]\\d{2}[2-9]\\d{6}$'); // true, 美国(正则)
      isMobile('2125551234', /^[2-9]\d{2}[2-9]\d{6}$/); // true, 美国(正则)
      isMobile('1125551234', /^[2-9]\d{2}[2-9]\d{6}$/); // false, 区号不能以 1 开头