Example #1
0
def validate(number):
    """Check if the number is a valid VAT number. This checks the length,
    formatting and check digit."""
    number = compact(number)
    if not all(x in _alphabet for x in number[:2]):
        raise InvalidFormat()
    if not isdigits(number[2:]):
        raise InvalidFormat()
    if len(number) != 11:
        raise InvalidLength()
    if number[2:5] != '000':
        # numbers from Monaco are valid TVA but not SIREN
        siren.validate(number[2:])
    if isdigits(number):
        # all-numeric digits
        if int(number[:2]) != (int(number[2:] + '12') % 97):
            raise InvalidChecksum()
    else:
        # one of the first two digits isn't a number
        if isdigits(number[0]):
            check = (
                _alphabet.index(number[0]) * 24 +
                _alphabet.index(number[1]) - 10)
        else:
            check = (
                _alphabet.index(number[0]) * 34 +
                _alphabet.index(number[1]) - 100)
        if (int(number[2:]) + 1 + check // 11) % 11 != (check % 11):
            raise InvalidChecksum()
    return number
Example #2
0
def validate(number):
    """Check if the number provided is a valid VAT number. This checks the
    length, formatting and check digit."""
    number = compact(number)
    if not isdigits(number[1:-1]):
        raise InvalidFormat()
    if len(number) != 9:
        raise InvalidLength()
    if number[0] in 'KLM':
        # K: Spanish younger than 14 year old
        # L: Spanish living outside Spain without DNI
        # M: granted the tax to foreigners who have no NIE
        # these use the old checkdigit algorithm (the DNI one)
        if number[-1] != dni.calc_check_digit(number[1:-1]):
            raise InvalidChecksum()
    elif isdigits(number[0]):
        # natural resident
        dni.validate(number)
    elif number[0] in 'XYZ':
        # foreign natural person
        nie.validate(number)
    else:
        # otherwise it has to be a valid CIF
        cif.validate(number)
    return number
Example #3
0
def validate(number):
    """Check if the number provided is a valid VAT number. This checks the
    length, formatting and check digit."""
    number = compact(number)
    if not isdigits(number[1:-1]):
        raise InvalidFormat()
    if len(number) != 9:
        raise InvalidLength()
    if number[0] in 'KLM':
        # K: Spanish younger than 14 year old
        # L: Spanish living outside Spain without DNI
        # M: granted the tax to foreigners who have no NIE
        # these use the old checkdigit algorithm (the DNI one)
        if number[-1] != dni.calc_check_digit(number[1:-1]):
            raise InvalidChecksum()
    elif isdigits(number[0]):
        # natural resident
        dni.validate(number)
    elif number[0] in 'XYZ':
        # foreign natural person
        nie.validate(number)
    else:
        # otherwise it has to be a valid CIF
        cif.validate(number)
    return number
Example #4
0
def validate(number):
    """Check if the number is a valid VAT number. This checks the length,
    formatting and check digit."""
    number = compact(number)
    if not all(x in _alphabet for x in number[:2]):
        raise InvalidFormat()
    if not isdigits(number[2:]):
        raise InvalidFormat()
    if len(number) != 11:
        raise InvalidLength()
    if number[2:5] != '000':
        # numbers from Monaco are valid TVA but not SIREN
        siren.validate(number[2:])
    if isdigits(number):
        # all-numeric digits
        if int(number[:2]) != (int(number[2:] + '12') % 97):
            raise InvalidChecksum()
    else:
        # one of the first two digits isn't a number
        if isdigits(number[0]):
            check = (_alphabet.index(number[0]) * 24 +
                     _alphabet.index(number[1]) - 10)
        else:
            check = (_alphabet.index(number[0]) * 34 +
                     _alphabet.index(number[1]) - 100)
        if (int(number[2:]) + 1 + check // 11) % 11 != (check % 11):
            raise InvalidChecksum()
    return number
Example #5
0
def validate(number):
    """Check if the number provided is a valid CAS RN."""
    number = compact(number)
    if not 7 <= len(number) <= 12:
        raise InvalidLength()
    if not isdigits(number[:-5]) or not isdigits(number[-4:-2]):
        raise InvalidFormat()
    if number[-2] != '-' or number[-5] != '-':
        raise InvalidFormat()
    if number[-1] != calc_check_digit(number[:-1]):
        raise InvalidChecksum()
    return number
Example #6
0
def validate(number):
    """Check if the number provided is a valid CAS RN."""
    number = compact(number)
    if not 7 <= len(number) <= 12:
        raise InvalidLength()
    if not isdigits(number[:-5]) or not isdigits(number[-4:-2]):
        raise InvalidFormat()
    if number[-2] != '-' or number[-5] != '-':
        raise InvalidFormat()
    if number[-1] != calc_check_digit(number[:-1]):
        raise InvalidChecksum()
    return number
Example #7
0
def validate(number):
    """Check if the number provided is valid. This checks the length
    and check digits."""
    number = compact(number)
    if not isdigits(number[:5]) or not isdigits(number[7:]):
        raise InvalidFormat()
    if not isdigits(number[5:7]) and number[5:7] not in ('2A', '2B'):
        raise InvalidFormat()
    if len(number) != 15:
        raise InvalidLength()
    if calc_check_digits(number) != number[13:]:
        raise InvalidChecksum()
    return number
Example #8
0
def validate(number):
    """Check if the number provided is valid. This checks the length
    and check digits."""
    number = compact(number)
    if not isdigits(number[:5]) or not isdigits(number[7:]):
        raise InvalidFormat()
    if not isdigits(number[5:7]) and number[5:7] not in ('2A', '2B'):
        raise InvalidFormat()
    if len(number) != 15:
        raise InvalidLength()
    if calc_check_digits(number) != number[13:]:
        raise InvalidChecksum()
    return number
Example #9
0
def validate(number):
    """Check if the number is valid. This checks the length and check
    digit."""
    number = compact(number)
    if not all(x in _alphabet for x in number):
        raise InvalidFormat()
    if len(number) != 7:
        raise InvalidLength()
    if isdigits(number[0]) and not isdigits(number):
        # new style SEDOLs are supposed to start with a letter, old-style
        # numbers should be fully numeric
        raise InvalidFormat()
    if calc_check_digit(number[:-1]) != number[-1]:
        raise InvalidChecksum()
    return number
Example #10
0
def validate(number):
    """Check if the number is a valid BN or BN15. This checks the length,
    formatting and check digit."""
    number = compact(number)
    if len(number) not in (9, 15):
        raise InvalidLength()
    if not isdigits(number[:9]):
        raise InvalidFormat()
    luhn.validate(number[:9])
    if len(number) == 15:
        if number[9:11] not in ('RC', 'RM', 'RP', 'RT'):
            raise InvalidComponent()
        if not isdigits(number[11:]):
            raise InvalidFormat()
    return number
Example #11
0
def validate(number):
    """Check if the number is valid. This checks the length and check
    digit."""
    number = compact(number)
    if not all(x in _alphabet for x in number):
        raise InvalidFormat()
    if len(number) != 7:
        raise InvalidLength()
    if isdigits(number[0]) and not isdigits(number):
        # new style SEDOLs are supposed to start with a letter, old-style
        # numbers should be fully numeric
        raise InvalidFormat()
    if calc_check_digit(number[:-1]) != number[-1]:
        raise InvalidChecksum()
    return number
Example #12
0
def validate(number):
    """Check if the number provided is a valid NCF."""
    number = compact(number)
    if len(number) == 11:
        if number[0] != 'B' or not isdigits(number[1:]):
            raise InvalidFormat()
    elif len(number) == 19:
        if number[0] not in 'AP' or not isdigits(number[1:]):
            raise InvalidFormat()
    else:
        raise InvalidLength()
    if number[-10:-8] not in (
            '01', '02', '03', '04', '11', '12', '13', '14', '15'):
        raise InvalidComponent()
    return number
Example #13
0
def validate(number):
    """Check if the number is a valid BN or BN15. This checks the length,
    formatting and check digit."""
    number = compact(number)
    if len(number) not in (9, 15):
        raise InvalidLength()
    if not isdigits(number[:9]):
        raise InvalidFormat()
    luhn.validate(number[:9])
    if len(number) == 15:
        if number[9:11] not in ('RC', 'RM', 'RP', 'RT'):
            raise InvalidComponent()
        if not isdigits(number[11:]):
            raise InvalidFormat()
    return number
Example #14
0
def validate(number):
    """Check if the number is a valid Guatemala NIT number.

    This checks the length, formatting and check digit.
    """
    number = compact(number)
    if len(number) < 2 or len(number) > 12:
        raise InvalidLength()
    if not isdigits(number[:-1]):
        raise InvalidFormat()
    if number[-1] != 'K' and not isdigits(number[-1]):
        raise InvalidFormat()
    if number[-1] != calc_check_digit(number[:-1]):
        raise InvalidChecksum()
    return number
Example #15
0
def validate(number):
    """Check if the number is a valid btw number. This checks the length,
    formatting and check digit."""
    number = compact(number)
    if not isdigits(number[:9]) or int(number[:9]) <= 0:
        raise InvalidFormat()
    if not isdigits(number[10:]) or int(number[10:]) <= 0:
        raise InvalidFormat()
    if len(number) != 12:
        raise InvalidLength()
    if number[9] != 'B':
        raise InvalidFormat()
    if not bsn.is_valid(number[:9]) and not mod_97_10.is_valid('NL' + number):
        raise InvalidChecksum()
    return number
Example #16
0
def validate(number):
    """Check if the number is a valid number. This checks the length,
    formatting and check digit."""
    number = compact(number)
    if len(number) != 9:
        raise InvalidLength()
    if not isdigits(number[2:]):
        raise InvalidFormat()
    if not isdigits(number[:2]) and not all(x in 'ABCEHKMOPT' for x in number[:2]):
        raise InvalidFormat()
    if number[0] not in '1234567ABCEHKM':
        raise InvalidComponent()
    if number[-1] != calc_check_digit(number):
        raise InvalidChecksum()
    return number
Example #17
0
def validate(number):
    """Check if the number provided is a valid RUC number. This checks the
    length, formatting, check digit and check sum."""
    number = compact(number)
    if len(number) != 13:
        raise InvalidLength()
    if not isdigits(number):
        raise InvalidFormat()
    if (number[:2] < '01' or number[:2] > '24') and (number[:2] not in ('30', '50')):
        raise InvalidComponent()  # invalid province code
    if number[2] < '6':
        # 0..5 = natural RUC: CI plus establishment number
        if number[-3:] == '000':
            raise InvalidComponent()  # establishment number wrong
        ci.validate(number[:10])
    elif number[2] == '6':
        # 6 = public RUC
        if number[-4:] == '0000':
            raise InvalidComponent()  # establishment number wrong
        if _checksum(number[:9], (3, 2, 7, 6, 5, 4, 3, 2, 1)) != 0:
            raise InvalidChecksum()
    elif number[2] == '9':
        # 9 = juridical RUC
        if number[-3:] == '000':
            raise InvalidComponent()  # establishment number wrong
        if _checksum(number[:10], (4, 3, 2, 7, 6, 5, 4, 3, 2, 1)) != 0:
            raise InvalidChecksum()
    else:
        raise InvalidComponent()  # third digit wrong
    return number
Example #18
0
def validate(number):
    """Check if the number provided is a valid FIGI."""
    number = compact(number)
    if not all(x in '0123456789BCDFGHJKLMNPQRSTVWXYZ' for x in number):
        raise InvalidFormat()
    if len(number) != 12:
        raise InvalidLength()
    if isdigits(number[0]) or isdigits(number[1]):
        raise InvalidFormat()
    if number[:2] in ('BS', 'BM', 'GG', 'GB', 'VG'):
        raise InvalidComponent()
    if number[2] != 'G':
        raise InvalidComponent()
    if calc_check_digit(number[:-1]) != number[-1]:
        raise InvalidChecksum()
    return number
Example #19
0
def validate(number):
    """Check if the number is a valid Costa Rica CPJ number.

    This checks the length and formatting.
    """
    number = compact(number)
    if len(number) != 10:
        raise InvalidLength()
    if not isdigits(number):
        raise InvalidFormat()
    if number[0] not in ('2', '3', '4', '5'):
        raise InvalidComponent()
    if number[0] == '2' and number[1:4] not in ('100', '200', '300', '400'):
        raise InvalidComponent()
    class_three_types = ('002', '003', '004', '005', '006', '007', '008',
                         '009', '010', '011', '012', '013', '014', '101',
                         '102', '103', '104', '105', '106', '107', '108',
                         '109', '110')
    if number[0] == '3' and number[1:4] not in class_three_types:
        raise InvalidComponent()
    if number[0] == '4' and number[1:4] != '000':
        raise InvalidComponent()
    if number[0] == '5' and number[1:4] != '001':
        raise InvalidComponent()
    return number
Example #20
0
def validate(number, strip_check_digit=True):
    """Check if the number is a valid MEID number. This converts the
    representation format of the number (if it is decimal it is not converted
    to hexadecimal)."""
    from stdnum import luhn
    # first parse the number
    number, cd = _parse(number)
    if len(number) == 18:
        # decimal format can be easily determined
        if cd:
            luhn.validate(number + cd)
        # convert to hex
        manufacturer_code = int(number[0:10])
        serial_num = int(number[10:18])
        if _bit_length(manufacturer_code) > 32 or _bit_length(serial_num) > 24:
            raise InvalidComponent()
        number = '%08X%06X' % (manufacturer_code, serial_num)
        cd = calc_check_digit(number)
    elif isdigits(number):
        # if the remaining hex format is fully decimal it is an IMEI number
        from stdnum import imei
        imei.validate(number + cd)
    else:
        # normal hex Luhn validation
        if cd:
            luhn.validate(number + cd, alphabet=_hex_alphabet)
    if strip_check_digit:
        cd = ''
    return number + cd
Example #21
0
def validate(number):
    """Check if the number provided is a valid RUC number. This checks the
    length, formatting, check digit and check sum."""
    number = compact(number)
    if len(number) != 13:
        raise InvalidLength()
    if not isdigits(number):
        raise InvalidFormat()
    if (number[:2] < '01' or number[:2] > '24') and (number[:2]
                                                     not in ('30', '50')):
        raise InvalidComponent()  # invalid province code
    if number[2] < '6':
        # 0..5 = natural RUC: CI plus establishment number
        _validate_natural(number)
    elif number[2] == '6':
        # 6 = public RUC (or natural RUC)
        try:
            _validate_public(number)
        except ValidationError:
            _validate_natural(number)
    elif number[2] == '9':
        # 9 = juridical RUC
        _validate_juridical(number)
    else:
        raise InvalidComponent()  # third digit wrong
    return number
Example #22
0
def validate(number):
    """Check if the number provided is a valid FIGI."""
    number = compact(number)
    if not all(x in '0123456789BCDFGHJKLMNPQRSTVWXYZ' for x in number):
        raise InvalidFormat()
    if len(number) != 12:
        raise InvalidLength()
    if isdigits(number[0]) or isdigits(number[1]):
        raise InvalidFormat()
    if number[:2] in ('BS', 'BM', 'GG', 'GB', 'VG'):
        raise InvalidComponent()
    if number[2] != 'G':
        raise InvalidComponent()
    if calc_check_digit(number[:-1]) != number[-1]:
        raise InvalidChecksum()
    return number
Example #23
0
def validate(number, strip_check_digit=True):
    """Check if the number is a valid MEID number. This converts the
    representation format of the number (if it is decimal it is not converted
    to hexadecimal)."""
    # first parse the number
    number, cd = _parse(number)
    from stdnum import luhn
    if len(number) == 18:
        # decimal format can be easily determined
        if cd:
            luhn.validate(number + cd)
        # convert to hex
        manufacturer_code = int(number[0:10])
        serial_num = int(number[10:18])
        if _bit_length(manufacturer_code) > 32 or _bit_length(serial_num) > 24:
            raise InvalidComponent()
        number = '%08X%06X' % (manufacturer_code, serial_num)
        cd = calc_check_digit(number)
    elif isdigits(number):
        # if the remaining hex format is fully decimal it is an IMEI number
        from stdnum import imei
        imei.validate(number + cd)
    else:
        # normal hex Luhn validation
        if cd:
            luhn.validate(number + cd, alphabet=_hex_alphabet)
    if strip_check_digit:
        cd = ''
    return number + cd
Example #24
0
def validate(number):
    """Check if the number is a valid DNI."""
    number = compact(number)
    if not isdigits(number):
        raise InvalidFormat()
    if len(number) not in (7, 8):
        raise InvalidLength()
    return number
Example #25
0
def validate(number):
    """Check if the number provided is a valid NIF."""
    number = compact(number)
    if not isdigits(number):
        raise InvalidFormat()
    if len(number) != 13:
        raise InvalidLength()
    return number
Example #26
0
def convert(number):
    """Convert an "old" style 8-digit VAT number where the second character
    is a letter to the new 8-digit format where only the last digit is a
    character."""
    number = compact(number)
    if len(number) == 8 and not isdigits(number[1]):
        number = '0' + number[2:7] + number[0] + number[7:]
    return number
Example #27
0
def validate(number):
    """Check if the number provided is a valid NIF."""
    number = compact(number)
    if not isdigits(number):
        raise InvalidFormat()
    if len(number) != 13:
        raise InvalidLength()
    return number
Example #28
0
def validate(number):
    """Check if the number is a valid VAT number. This checks the length,
    formatting and check digit."""
    number = compact(number)
    if not isdigits(number) or number[-2:] != '01':
        raise InvalidFormat()
    orgnr.validate(number[:-2])
    return number
Example #29
0
def validate(number):
    """Check if the number is a valid VAT number. This checks the length,
    formatting and check digit."""
    number = compact(number)
    if not isdigits(number) or number[-2:] != '01':
        raise InvalidFormat()
    orgnr.validate(number[:-2])
    return number
Example #30
0
def validate(number):
    """Check if the number is a valid DNI."""
    number = compact(number)
    if not isdigits(number):
        raise InvalidFormat()
    if len(number) not in (7, 8):
        raise InvalidLength()
    return number
Example #31
0
def validate(number):
    """Check if the number provided is a valid VAT number. This checks the
    length and formatting."""
    number = compact(number)
    if not isdigits(number):
        raise InvalidFormat()
    if len(number) not in (5, 6):
        raise InvalidLength()
    return number
Example #32
0
def validate(number):
    """Check if the number is a valid Indonesia NPWP number."""
    number = compact(number)
    if len(number) != 15:
        raise InvalidLength()
    if not isdigits(number):
        raise InvalidFormat()
    luhn.validate(number[:9])
    return number
Example #33
0
def validate(number):
    """Check if the number is in the correct format. This currently does not
    check whether the code corresponds to a real address."""
    number = compact(number)
    if not isdigits(number) or number.startswith('0'):
        raise InvalidFormat()
    if len(number) != 5:
        raise InvalidLength()
    return number
Example #34
0
def validate(number):
    """Check if the number is a valid SIN. This checks the length, formatting
    and check digit."""
    number = compact(number)
    if len(number) != 9:
        raise InvalidLength()
    if not isdigits(number):
        raise InvalidFormat()
    return luhn.validate(number)
Example #35
0
def validate(number):
    """Check if the given fiscal code is valid."""
    number = compact(number)
    if len(number) < 4 or len(number) > 12:
        raise InvalidLength()
    if not isdigits(number):
        raise InvalidFormat()

    return number
Example #36
0
def validate(number):
    """Check if the number is a valid SIN. This checks the length, formatting
    and check digit."""
    number = compact(number)
    if len(number) != 9:
        raise InvalidLength()
    if not isdigits(number):
        raise InvalidFormat()
    return luhn.validate(number)
Example #37
0
def validate(number):
    """Check if the number is a valid VAT number. This checks the length,
    formatting and check digit."""
    number = compact(number)
    if len(number) == 5:
        if not isdigits(number[2:]):
            raise InvalidFormat()
        if number.startswith('GD') and int(number[2:]) < 500:
            # government department
            pass
        elif number.startswith('HA') and int(number[2:]) >= 500:
            # health authority
            pass
        else:
            raise InvalidComponent()
    elif len(number) == 11 and number[0:6] in ('GD8888', 'HA8888'):
        if not isdigits(number[6:]):
            raise InvalidFormat()
        if number.startswith('GD') and int(number[6:9]) < 500:
            # government department
            pass
        elif number.startswith('HA') and int(number[6:9]) >= 500:
            # health authority
            pass
        else:
            raise InvalidComponent()
        if int(number[6:9]) % 97 != int(number[9:11]):
            raise InvalidChecksum()
    elif len(number) in (9, 12):
        if not isdigits(number):
            raise InvalidFormat()
        # standard number: nnn nnnn nn
        # branch trader: nnn nnnn nn nnn (ignore the last thee digits)
        # restarting: 100 nnnn nn
        if int(number[:3]) >= 100:
            if checksum(number[:9]) not in (0, 42, 55):
                raise InvalidChecksum()
        else:
            if checksum(number[:9]) != 0:
                raise InvalidChecksum()
    else:
        raise InvalidLength()
    return number
def validate(number):
    """Check if the number is a valid identity number."""
    number = compact(number)
    if len(number) not in (10, 12):
        raise InvalidLength()
    if not isdigits(number):
        raise InvalidFormat()
    get_birth_date(number)
    luhn.validate(number[-10:])
    return number
Example #39
0
def validate(number):
    """Check if the number is a valid NI. This checks the length, formatting
    and check digit."""
    number = compact(number)
    if len(number) != 11:
        raise InvalidLength()
    if not isdigits(number):
        raise InvalidFormat()
    get_birth_date(number)
    return number
Example #40
0
def validate(number):
    """Check if the number is a valid CUIT."""
    number = compact(number)
    if len(number) != 11:
        raise InvalidLength()
    if not isdigits(number):
        raise InvalidFormat()
    if calc_check_digit(number[:-1]) != number[-1]:
        raise InvalidChecksum()
    return number
Example #41
0
def validate(number):
    """Check if the number provided is a valid ID. This checks the length,
    formatting and check digit."""
    number = compact(number)
    if len(number) > 9:
        raise InvalidLength()
    if not isdigits(number) or int(number) <= 0:
        raise InvalidFormat()
    luhn.validate(number)
    return number
Example #42
0
def validate(number):
    """Check if the number provided is a valid CUPS. This checks length,
    formatting and check digits."""
    number = compact(number)
    if len(number) not in (20, 22):
        raise InvalidLength()
    if number[:2] != 'ES':
        raise InvalidComponent()
    if not isdigits(number[2:18]):
        raise InvalidFormat()
    if number[20:]:
        pnumber, ptype = number[20:]
        if not isdigits(pnumber):
            raise InvalidFormat()
        if ptype not in 'FPRCXYZ':
            raise InvalidFormat()
    if calc_check_digits(number) != number[18:20]:
        raise InvalidChecksum()
    return number
Example #43
0
def validate(number):
    """Check if the number provided is a valid CCC."""
    number = compact(number)
    if len(number) != 20:
        raise InvalidLength()
    if not isdigits(number):
        raise InvalidFormat()
    if number[8:10] != calc_check_digits(number):
        raise InvalidChecksum()
    return number
Example #44
0
def validate(number):
    """Check if the number provided is a valid VAT number. This checks the
    length, formatting and check digit."""
    number = compact(number)
    if not isdigits(number) or number[0] == '0':
        raise InvalidFormat()
    if len(number) != 9:
        raise InvalidLength()
    mod_11_10.validate(number)
    return number
Example #45
0
def validate(number):
    """Check if the number provided is a valid cedula."""
    number = compact(number)
    if not isdigits(number):
        raise InvalidFormat()
    if number in whitelist:
        return number
    if len(number) != 11:
        raise InvalidLength()
    return luhn.validate(number)
Example #46
0
def validate(number):
    """Check if the number is a valid CUIT."""
    number = compact(number)
    if len(number) != 11:
        raise InvalidLength()
    if not isdigits(number):
        raise InvalidFormat()
    if calc_check_digit(number[:-1]) != number[-1]:
        raise InvalidChecksum()
    return number
Example #47
0
def validate(number):
    """Check if the number is a valid OIB number. This checks the length,
    formatting and check digit."""
    number = compact(number)
    if not isdigits(number):
        raise InvalidFormat()
    if len(number) != 11:
        raise InvalidLength()
    mod_11_10.validate(number)
    return number
Example #48
0
def calc_check_digit(number):
    """Calculate the check digit for the number. The number should not
    already have a check digit."""
    # both the 18-digit decimal format and the 14-digit hex format
    # containing only decimal digits should use the decimal Luhn check
    from stdnum import luhn
    if isdigits(number):
        return luhn.calc_check_digit(number)
    else:
        return luhn.calc_check_digit(number, alphabet=_hex_alphabet)
Example #49
0
def calc_check_digit(number):
    """Calculate the check digit for the number. The number should not
    already have a check digit."""
    # both the 18-digit decimal format and the 14-digit hex format
    # containing only decimal digits should use the decimal Luhn check
    from stdnum import luhn
    if isdigits(number):
        return luhn.calc_check_digit(number)
    else:
        return luhn.calc_check_digit(number, alphabet=_hex_alphabet)
Example #50
0
def validate(number):
    """Check if the number is a valid NI. This checks the length, formatting
    and check digit."""
    number = compact(number)
    if len(number) != 11:
        raise InvalidLength()
    if not isdigits(number):
        raise InvalidFormat()
    get_birth_date(number)
    return number
Example #51
0
def validate(number):
    """Check if the number provided is a valid ID. This checks the length,
    formatting and check digit."""
    number = compact(number)
    if len(number) > 9:
        raise InvalidLength()
    if not isdigits(number) or int(number) <= 0:
        raise InvalidFormat()
    luhn.validate(number)
    return number
def validate(number):
    """Check if the number is a valid postal code."""
    number = compact(number)
    if not isdigits(number):
        raise InvalidFormat()
    if len(number) != 4:
        raise InvalidLength()
    if not info(number):
        raise InvalidComponent()
    return number
Example #53
0
def validate(number):
    """Check if the number is a valid VAT number. This checks the length,
    formatting and check digit."""
    number = compact(number)
    if not isdigits(number):
        raise InvalidFormat()
    if len(number) != 10:
        raise InvalidLength()
    if checksum(number) != 0:
        raise InvalidChecksum()
    return number
Example #54
0
def validate(number):
    """Check if the number is a valid ISSN. This checks the length and
    whether the check digit is correct."""
    number = compact(number)
    if not isdigits(number[:-1]):
        raise InvalidFormat()
    if len(number) != 8:
        raise InvalidLength()
    if calc_check_digit(number[:-1]) != number[-1]:
        raise InvalidChecksum()
    return number
Example #55
0
def validate(number):
    """Check if the number provided is a valid NIE. This checks the length,
    formatting and check digit."""
    number = compact(number)
    if not isdigits(number[1:-1]) or number[:1] not in 'XYZ':
        raise InvalidFormat()
    if len(number) != 9:
        raise InvalidLength()
    if calc_check_digit(number[:-1]) != number[-1]:
        raise InvalidChecksum()
    return number
Example #56
0
def validate(number):
    """Check if the number is a valid COE. This checks the length and
    formatting."""
    number = compact(number)
    if len(number) > 5 or len(number) == 0:
        raise InvalidLength()
    if not isdigits(number):
        raise InvalidFormat()
    if len(number) < 3 and int(number) not in _lownumbers:
        raise InvalidComponent()
    return number
Example #57
0
def validate(number):
    """Check if the number is a valid RUT. This checks the length, formatting
    and check digit."""
    number = compact(number)
    if len(number) not in (8, 9):
        raise InvalidLength()
    if not isdigits(number[:-1]):
        raise InvalidFormat()
    if number[-1] != calc_check_digit(number[:-1]):
        raise InvalidChecksum()
    return number
Example #58
0
def validate(number):
    """Check if the number is a valid ABN. This checks the length, formatting
    and check digit."""
    number = compact(number)
    if not isdigits(number):
        raise InvalidFormat()
    if len(number) != 11:
        raise InvalidLength()
    if calc_check_digits(number[2:]) != number[:2]:
        raise InvalidChecksum()
    return number
Example #59
0
def validate(number):
    """Check if the number is a valid VAT number. This checks the length,
    formatting and check digit."""
    number = compact(number)
    if not isdigits(number) or number.startswith('0'):
        raise InvalidFormat()
    if len(number) != 8:
        raise InvalidLength()
    if calc_check_digit(number[:-1]) != number[-1]:
        raise InvalidChecksum()
    return number