How to use the phonenumbers.is_valid_number function in phonenumbers

To help you get started, we’ve selected a few phonenumbers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Sotera / pst-extraction / spark / phone_numbers.py View on Github external
if excerpt_start < 0:
            excerpt_start = 0
        excerpt_prefix = source_txt[ excerpt_start : match.start() ]

        phone_number_obj = None

        try:
            # print "phone guess: %s"%value_normalized
            # Try using the 'phonenumbers' module to parse the number. Note that we need to prefix
            # the phone number string with "+" for parse() to work properly. If the number can't
            # be parsed it will throw a NumberParseException.
            phone_number_obj = phonenumbers.parse(u'+'+value_normalized, None)

            # More lenient than valid_num
            possible_num = phonenumbers.is_possible_number(phone_number_obj)
            valid_num= phonenumbers.is_valid_number(phone_number_obj)

            # print "possible=%s valid=%s"%(str(possible_num), str(valid_num))
            # If the phonenumbers module thinks the number is invalid BUT it is preceded by text
            # with a phone-related keyword, we'll accept the number. If the number is invalid and
            # doesn't have a phone-related keyword, however, skip it.
            if (not possible_num or not valid_num) and not contains_tel_keyword(excerpt_prefix[-15:]):
                continue

        except phonenumbers.phonenumberutil.NumberParseException as err:

            # The phonenumbers modules couldn't parse the number; however, if it's preceded by text
            # with a phone-related keyword we'll still accept it. Or put another way, if it is NOT
            # preceded by a phone keyword, skip it.
            if not contains_tel_keyword(excerpt_prefix[-15:]):
                continue
github fabiocaccamo / python-benedict / benedict / dicts / parse / parse_util.py View on Github external
def _parse_phonenumber(val, country_code=None):
    try:
        phone_obj = phonenumbers.parse(val, country_code)
        if phonenumbers.is_valid_number(phone_obj):
            return {
                'e164': phonenumbers.format_number(
                    phone_obj, PhoneNumberFormat.E164),
                'international': phonenumbers.format_number(
                    phone_obj, PhoneNumberFormat.INTERNATIONAL),
                'national': phonenumbers.format_number(
                    phone_obj, PhoneNumberFormat.NATIONAL),
            }
        return None
    except phonenumberutil.NumberParseException:
        return None
github gotlium / django-secure-auth / secureauth / utils / __init__.py View on Github external
def is_phone(phone):
    try:
        return phonenumbers.is_valid_number(phonenumbers.parse(phone, None))
    except phonenumbers.NumberParseException:
        return False
github AamAadmiParty / cleansweep / cleansweep / forms.py View on Github external
def validate_phone(self, field):
        if len(field.data) != 10:
            raise validators.ValidationError('it should be 10 digit only')

        phone = field.data.strip()

        try:
            number = phonenumbers.parse(phone, "IN")
        except Exception:
            raise validators.ValidationError('Please enter number only')

        if phonenumbers.is_valid_number(number) == False:
            raise validators.ValidationError('Invalid Phone number')

        if models.Member.find(phone=phone):
            raise validators.ValidationError('This phone number is already used')
github kvesteri / sqlalchemy-utils / sqlalchemy_utils / types.py View on Github external
def is_valid_number(self):
        return phonenumbers.is_valid_number(self._phone_number)
github mattmakai / callbot / callbot.py View on Github external
def validate_phone_numbers(phone_numbers):
    """
        Uses the python-phonenumbers library to make sure each phone number
        is in a valid format.
    """
    invalid_response = " is not a valid phone number format. Please " + \
                       "correct the number and retry. No calls have yet " + \
                       "been dialed."
    for phone_number in phone_numbers:
        try:
            validate_phone_number = phonenumbers.parse(phone_number)
            if not phonenumbers.is_valid_number(validate_phone_number):
                return False, phone_number + invalid_response
        except:
            return False, phone_number + invalid_response
    return True, None
github entrepreneur-interet-general / the-magical-csv-merge-machine / merge_machine / preprocess_fields_v3.py View on Github external
def score_phone_number(z): return 100 if phonenumbers.is_valid_number(z) else 75 if phonenumbers.is_possible_number(z) else 5
github tableau / VizAlerts / vizalert / smsaction.py View on Github external
except phonenumbers.NumberParseException as e:
            errormessage = u'SMS Unable to parse number {}. Error: {}'.format(smsnumber, e.message)
            log.logger.error(errormessage)
            raise UserWarning(errormessage)

        try:
            if not phonenumbers.is_possible_number(smsnumber_obj):
                errormessage = u'SMS Number is not possibly valid: {}.'.format(smsnumber)
                log.logger.error(errormessage)
                raise UserWarning(errormessage)
        except phonenumbers.NumberParseException as e:
            errormessage = u'SMS Unable to parse number {}. Error: {}'.format(smsnumber, e.message)
            log.logger.error(errormessage)
            raise UserWarning(errormessage)

        if not phonenumbers.is_valid_number(smsnumber_obj):
            errormessage = u'SMS Number is not valid: {}.'.format(smsnumber)
            log.logger.error(errormessage)
            raise UserWarning(errormessage)


        e164_number = phonenumbers.format_number(smsnumber_obj, phonenumbers.PhoneNumberFormat.E164)
        if not e164_number:
            errormessage = u'SMS number {} could not be converted to E.164 for an unknown reason.'.format(smsnumber)
            log.logger.error(errormessage)
            raise UserWarning(errormessage)

        # all good, return it!
        return e164_number
    except Exception as e:
        log.logger.error(e.message)
        return None
github django-oscar / django-oscar / src / oscar / core / phonenumber.py View on Github external
def is_valid(self):
        """
        checks whether the number supplied is actually valid
        """
        return phonenumbers.is_valid_number(self)
github systers / vms / vms / registration / phone_validate.py View on Github external
def validate_phone(my_country, my_phone):
    try:
        entry = Country.objects.get(name__iexact=my_country)
        country_code = entry.code2
    except:
        print('No matching country in database')
        return "missing"
    print(country_code)
    parsed_number = phonenumbers.parse(my_phone, country_code)
    return (phonenumbers.is_valid_number(parsed_number)
            and phonenumbers.is_possible_number(parsed_number))