當前位置: 首頁>>代碼示例>>Python>>正文


Python phonenumbers.is_valid_number方法代碼示例

本文整理匯總了Python中phonenumbers.is_valid_number方法的典型用法代碼示例。如果您正苦於以下問題:Python phonenumbers.is_valid_number方法的具體用法?Python phonenumbers.is_valid_number怎麽用?Python phonenumbers.is_valid_number使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在phonenumbers的用法示例。


在下文中一共展示了phonenumbers.is_valid_number方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_canonical_form

# 需要導入模塊: import phonenumbers [as 別名]
# 或者: from phonenumbers import is_valid_number [as 別名]
def get_canonical_form(self, input_data):
        """ Validate and return a canonical form to be stored in DB
        and compared against.
        Returns ``None`` if input isn't a valid phone number.
        """
        import phonenumbers

        try:
            z = phonenumbers.parse(
                input_data, region=config_value("PHONE_REGION_DEFAULT")
            )
            if phonenumbers.is_valid_number(z):
                return phonenumbers.format_number(
                    z, phonenumbers.PhoneNumberFormat.E164
                )
            return None
        except phonenumbers.phonenumberutil.NumberParseException:
            return None 
開發者ID:Flask-Middleware,項目名稱:flask-security,代碼行數:20,代碼來源:phone_util.py

示例2: validate_phone

# 需要導入模塊: import phonenumbers [as 別名]
# 或者: from phonenumbers import is_valid_number [as 別名]
def validate_phone(cls, number):
        """
        Validates the given phone number which should be in E164 format.
        """
        try:
            parsed = phonenumbers.parse(number)
        except phonenumbers.NumberParseException as e:
            raise InvalidURN(str(e))

        if number != phonenumbers.format_number(parsed, phonenumbers.PhoneNumberFormat.E164):
            raise InvalidURN("Phone numbers must be in E164 format")

        if not phonenumbers.is_possible_number(parsed) or not phonenumbers.is_valid_number(parsed):
            raise InvalidURN("Phone numbers must be in E164 format")

        return True 
開發者ID:rapidpro,項目名稱:casepro,代碼行數:18,代碼來源:models.py

示例3: __call__

# 需要導入模塊: import phonenumbers [as 別名]
# 或者: from phonenumbers import is_valid_number [as 別名]
def __call__(self, node, value):
        try:
            phone = phonenumbers.parse(value)
        except phonenumbers.NumberParseException:
            raise colander.Invalid(
                node,
                _(
                    u"Phone must be in format +XXXXXXXXXXX "
                    u"and contains country code",
                    mapping={'val': value}
                )
            )
        if not phonenumbers.is_valid_number(phone):
            raise colander.Invalid(
                node,
                _(
                    u"Phone is not valid",
                    mapping={'val': value}
                )
            ) 
開發者ID:mazvv,項目名稱:travelcrm,代碼行數:22,代碼來源:__init__.py

示例4: _parse_phone_number

# 需要導入模塊: import phonenumbers [as 別名]
# 或者: from phonenumbers import is_valid_number [as 別名]
def _parse_phone_number(cls, phone_number_string):
        """
        Parses a phone number string and raises proper exceptions in case it is invalid

        Args:
            phone_number_string (str): a string representing a phone number

        Returns:
            phonenumbers.phonenumber.PhoneNumber: a PhoneNumber object
        """
        try:
            phone_number = phonenumbers.parse(phone_number_string)
        except phonenumbers.phonenumberutil.NumberParseException:
            raise InvalidProfileDataException('Stored phone number is in an invalid string')
        if not phonenumbers.is_valid_number(phone_number):
            raise InvalidProfileDataException('Stored phone number is in an invalid phone number')
        return phone_number 
開發者ID:mitodl,項目名稱:micromasters,代碼行數:19,代碼來源:writers.py

示例5: clean_international_phonenumber

# 需要導入模塊: import phonenumbers [as 別名]
# 或者: from phonenumbers import is_valid_number [as 別名]
def clean_international_phonenumber(value):
    """
    Validate phone number taken from TelPrefixInput and return in format suitable for our DB.
    """
    invalid_number_message = _(u'The phone number entered is not valid.')

    try:
        num = phonenumbers.parse(value)
        if not phonenumbers.is_valid_number(num):
            raise forms.ValidationError(invalid_number_message)
    except phonenumbers.NumberParseException:
        raise forms.ValidationError(invalid_number_message)

    return phonenumbers.format_number(num, phonenumbers.PhoneNumberFormat.E164)


# noinspection PyAbstractClass 
開發者ID:erigones,項目名稱:esdc-ce,代碼行數:19,代碼來源:widgets.py

示例6: phone_number_is_valid

# 需要導入模塊: import phonenumbers [as 別名]
# 或者: from phonenumbers import is_valid_number [as 別名]
def phone_number_is_valid(number, country=None):
    """Given a phone number and a country code, returns True if the phone number is valid, otherwise False."""
    ensure_definition(number, country)
    if country is None:
        country = get_country()
    if isinstance(number, str):
        m = re.search(r'^whatsapp:(.*)', number)
        if m:
            number = m.group(1)
    try:
        pn = phonenumbers.parse(number, country)
    except:
        return False
    if phonenumbers.is_possible_number(pn) and phonenumbers.is_valid_number(pn):
        return True
    return False 
開發者ID:jhpyle,項目名稱:docassemble,代碼行數:18,代碼來源:functions.py

示例7: clean

# 需要導入模塊: import phonenumbers [as 別名]
# 或者: from phonenumbers import is_valid_number [as 別名]
def clean(self):
        data = self.cleaned_data

        phone_number = (data['phone_country_code'] or '') + \
            (data['phone_number'] or '')

        if data['phone_country_code'] and data['phone_number']:
            phone_number = data['phone_country_code'] + data['phone_number']
            try:
                phone_number = phonenumbers.parse(phone_number, None)
                if not phonenumbers.is_valid_number(phone_number):
                    self.add_error('phone_number', 'Invalid phone number')
            except phonenumbers.NumberParseException as e:
                self.add_error('phone_number', e)

        if data['pushbullet_access_token']:
            pushbullet_access_token = data['pushbullet_access_token']
            try:
                Pushbullet(pushbullet_access_token)
            except PushbulletError:
                self.add_error('pushbullet_access_token',
                               'Invalid pushbullet access token.')

        data['telegram_chat_id'] = data['telegram_chat_id'] if data['telegram_chat_id'] else None 
開發者ID:TheSpaghettiDetective,項目名稱:TheSpaghettiDetective,代碼行數:26,代碼來源:forms.py

示例8: validate_phone_number

# 需要導入模塊: import phonenumbers [as 別名]
# 或者: from phonenumbers import is_valid_number [as 別名]
def validate_phone_number(self, input_data):
        """ Return ``None`` if a valid phone number else an error message. """
        import phonenumbers

        try:
            z = phonenumbers.parse(
                input_data, region=config_value("PHONE_REGION_DEFAULT")
            )
            if phonenumbers.is_valid_number(z):
                return None
        except phonenumbers.phonenumberutil.NumberParseException:
            pass
        return get_message("PHONE_INVALID")[0] 
開發者ID:Flask-Middleware,項目名稱:flask-security,代碼行數:15,代碼來源:phone_util.py

示例9: localScan

# 需要導入模塊: import phonenumbers [as 別名]
# 或者: from phonenumbers import is_valid_number [as 別名]
def localScan(InputNumber):
    global number
    global localNumber
    global internationalNumber
    global numberCountryCode
    global numberCountry

    print(code_info + 'Running local scan...')

    FormattedPhoneNumber = "+" + formatNumber(InputNumber)

    try:
        PhoneNumberObject = phonenumbers.parse(FormattedPhoneNumber, None)
    except:
        return False
    else:
        if not phonenumbers.is_valid_number(PhoneNumberObject):
            return False

        number = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.E164).replace('+', '')
        numberCountryCode = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL).split(' ')[0]

        countryRequest = json.loads(requests.request('GET', 'https://restcountries.eu/rest/v2/callingcode/{}'.format(numberCountryCode.replace('+', ''))).content)
        numberCountry = countryRequest[0]['alpha2Code']

        localNumber = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.E164).replace(numberCountryCode, '')
        internationalNumber = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL)

        print(code_result + 'International format: {}'.format(internationalNumber))
        print(code_result + 'Local format: 0{}'.format(localNumber))
        print(code_result + 'Country code: {}'.format(numberCountryCode))
        print(code_result + 'Location: {}'.format(geocoder.description_for_number(PhoneNumberObject, "en")))
        print(code_result + 'Carrier: {}'.format(carrier.name_for_number(PhoneNumberObject, 'en')))
        print(code_result + 'Area: {}'.format(geocoder.description_for_number(PhoneNumberObject, 'en')))
        for timezoneResult in timezone.time_zones_for_number(PhoneNumberObject):
            print(code_result + 'Timezone: {}'.format(timezoneResult))

        if phonenumbers.is_possible_number(PhoneNumberObject):
            print(code_info + 'The number is valid and possible.')
        else:
            print(code_warning + 'The number is valid but might not be possible.') 
開發者ID:la-deep-web,項目名稱:Phoneinfoga,代碼行數:43,代碼來源:phoneinfoga.py

示例10: validate_phone_number

# 需要導入模塊: import phonenumbers [as 別名]
# 或者: from phonenumbers import is_valid_number [as 別名]
def validate_phone_number(number):
    obj = phonenumbers.parse(number, "UG")
    if not phonenumbers.is_valid_number(obj):
        raise ValidationError("Invalid Phone number {0}".format(number))
    if (carrier.name_for_number(obj, "en") != "MTN"):
        raise ValidationError(
            "{0}: Only MTN is supported at the moment".format(number))
    return "256{0}".format(obj.national_number) 
開發者ID:sparkplug,項目名稱:momoapi-python,代碼行數:10,代碼來源:utils.py

示例11: parse_phone_number

# 需要導入模塊: import phonenumbers [as 別名]
# 或者: from phonenumbers import is_valid_number [as 別名]
def parse_phone_number(self, raw_string=None, country="US"):
        raw_string = raw_string or self.get_current_value()
        if raw_string:
            parsed_num = phonenumbers.parse(raw_string, country)
            if not phonenumbers.is_valid_number(parsed_num):
                raise exceptions.InvalidPhoneNumberException(
                    "'{}' is not a valid phone number".format(raw_string))
            return parsed_num
        return self.empty_value 
開發者ID:codeforamerica,項目名稱:intake,代碼行數:11,代碼來源:field_types.py

示例12: is_valid

# 需要導入模塊: import phonenumbers [as 別名]
# 或者: from phonenumbers import is_valid_number [as 別名]
def is_valid(self):
        """
        checks whether the number supplied is actually valid
        """
        return phonenumbers.is_valid_number(self) 
開發者ID:hjlarry,項目名稱:flask-shop,代碼行數:7,代碼來源:utils.py

示例13: clean

# 需要導入模塊: import phonenumbers [as 別名]
# 或者: from phonenumbers import is_valid_number [as 別名]
def clean(self):
        data = self.cleaned_data
        if data['password'] != data['confirm_password']:
            self.add_error(
                'password',
                'Password and confirmation did not match'
            )

        phone_number = data['country_code'] + data['phone_number']
        try:
            phone_number = phonenumbers.parse(phone_number, None)
            if not phonenumbers.is_valid_number(phone_number):
                self.add_error('phone_number', 'Invalid phone number')
        except NumberParseException as e:
            self.add_error('phone_number', e) 
開發者ID:TwilioDevEd,項目名稱:account-security-quickstart-django,代碼行數:17,代碼來源:forms.py

示例14: clean

# 需要導入模塊: import phonenumbers [as 別名]
# 或者: from phonenumbers import is_valid_number [as 別名]
def clean(self):
        data = self.cleaned_data
        phone_number = data['country_code'] + data['phone_number']
        try:
            phone_number = phonenumbers.parse(phone_number, None)
            if not phonenumbers.is_valid_number(phone_number):
                self.add_error('phone_number', 'Invalid phone number')
        except NumberParseException as e:
            self.add_error('phone_number', e) 
開發者ID:TwilioDevEd,項目名稱:account-security-quickstart-django,代碼行數:11,代碼來源:forms.py

示例15: localScan

# 需要導入模塊: import phonenumbers [as 別名]
# 或者: from phonenumbers import is_valid_number [as 別名]
def localScan(InputNumber, print_results=True):
    print("Running local scan...")

    FormattedPhoneNumber = "+" + formatNumber(InputNumber)

    try:
        PhoneNumberObject = phonenumbers.parse(FormattedPhoneNumber, None)
    except Exception as e:
        print(e)
    else:
        if not phonenumbers.is_valid_number(PhoneNumberObject):
            return False

        number = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.E164).replace("+", "")
        numberCountryCode = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL).split(" ")[0]
        numberCountry = phonenumbers.region_code_for_country_code(int(numberCountryCode))

        localNumber = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.E164).replace(numberCountryCode, "")
        internationalNumber = phonenumbers.format_number(PhoneNumberObject, phonenumbers.PhoneNumberFormat.INTERNATIONAL)

        country = geocoder.country_name_for_number(PhoneNumberObject, "en")
        location = geocoder.description_for_number(PhoneNumberObject, "en")
        carrierName = carrier.name_for_number(PhoneNumberObject, "en")

        if print_results:
            print("International format: {}".format(internationalNumber))
            print("Local format: {}".format(localNumber))
            print("Country found: {} ({})".format(country, numberCountryCode))
            print("City/Area: {}".format(location))
            print("Carrier: {}".format(carrierName))
            for timezoneResult in timezone.time_zones_for_number(PhoneNumberObject):
                print("Timezone: {}".format(timezoneResult))

            if phonenumbers.is_possible_number(PhoneNumberObject):
                print("The number is valid and possible.")
            else:
                print("The number is valid but might not be possible.")

    numberObj = {}
    numberObj["input"] = InputNumber
    numberObj["default"] = number
    numberObj["local"] = localNumber
    numberObj["international"] = internationalNumber
    numberObj["country"] = country
    numberObj["countryCode"] = numberCountryCode
    numberObj["countryIsoCode"] = numberCountry
    numberObj["location"] = location
    numberObj["carrier"] = carrierName

    return numberObj 
開發者ID:rpotter12,項目名稱:whatsapp-play,代碼行數:52,代碼來源:target_info.py


注:本文中的phonenumbers.is_valid_number方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。