Skip to content

card

validators.card.amex(value)

Return whether or not given value is a valid American Express card number.

Examples:

>>> amex('378282246310005')
# Output: True
>>> amex('4242424242424242')
# Output: ValidationError(func=amex, args={'value': '4242424242424242'})

Parameters:

Name Type Description Default
value str

American Express card number string to validate

required

Returns:

Type Description
Literal[True]

If value is a valid American Express card number.

ValidationError

If value is an invalid American Express card number.

Source code in src/validators/card.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
@validator
def amex(value: str, /):
    """Return whether or not given value is a valid American Express card number.

    Examples:
        >>> amex('378282246310005')
        # Output: True
        >>> amex('4242424242424242')
        # Output: ValidationError(func=amex, args={'value': '4242424242424242'})

    Args:
        value:
            American Express card number string to validate

    Returns:
        (Literal[True]): If `value` is a valid American Express card number.
        (ValidationError): If `value` is an invalid American Express card number.
    """
    pattern = re.compile(r"^(34|37)")
    return card_number(value) and len(value) == 15 and pattern.match(value)

validators.card.card_number(value)

Return whether or not given value is a valid generic card number.

This validator is based on Luhn's algorithm.

Examples:

>>> card_number('4242424242424242')
# Output: True
>>> card_number('4242424242424241')
# Output: ValidationError(func=card_number, args={'value': '4242424242424241'})

Parameters:

Name Type Description Default
value str

Generic card number string to validate

required

Returns:

Type Description
Literal[True]

If value is a valid generic card number.

ValidationError

If value is an invalid generic card number.

Source code in src/validators/card.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@validator
def card_number(value: str, /):
    """Return whether or not given value is a valid generic card number.

    This validator is based on [Luhn's algorithm][1].

    [1]: https://github.com/mmcloughlin/luhn

    Examples:
        >>> card_number('4242424242424242')
        # Output: True
        >>> card_number('4242424242424241')
        # Output: ValidationError(func=card_number, args={'value': '4242424242424241'})

    Args:
        value:
            Generic card number string to validate

    Returns:
        (Literal[True]): If `value` is a valid generic card number.
        (ValidationError): If `value` is an invalid generic card number.
    """
    if not value:
        return False
    try:
        digits = list(map(int, value))
        odd_sum = sum(digits[-1::-2])
        even_sum = sum(sum(divmod(2 * d, 10)) for d in digits[-2::-2])
        return (odd_sum + even_sum) % 10 == 0
    except ValueError:
        return False

validators.card.diners(value)

Return whether or not given value is a valid Diners Club card number.

Examples:

>>> diners('3056930009020004')
# Output: True
>>> diners('4242424242424242')
# Output: ValidationError(func=diners, args={'value': '4242424242424242'})

Parameters:

Name Type Description Default
value str

Diners Club card number string to validate

required

Returns:

Type Description
Literal[True]

If value is a valid Diners Club card number.

ValidationError

If value is an invalid Diners Club card number.

Source code in src/validators/card.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
@validator
def diners(value: str, /):
    """Return whether or not given value is a valid Diners Club card number.

    Examples:
        >>> diners('3056930009020004')
        # Output: True
        >>> diners('4242424242424242')
        # Output: ValidationError(func=diners, args={'value': '4242424242424242'})

    Args:
        value:
            Diners Club card number string to validate

    Returns:
        (Literal[True]): If `value` is a valid Diners Club card number.
        (ValidationError): If `value` is an invalid Diners Club card number.
    """
    pattern = re.compile(r"^(30|36|38|39)")
    return card_number(value) and len(value) in {14, 16} and pattern.match(value)

validators.card.discover(value)

Return whether or not given value is a valid Discover card number.

Examples:

>>> discover('6011111111111117')
# Output: True
>>> discover('4242424242424242')
# Output: ValidationError(func=discover, args={'value': '4242424242424242'})

Parameters:

Name Type Description Default
value str

Discover card number string to validate

required

Returns:

Type Description
Literal[True]

If value is a valid Discover card number.

ValidationError

If value is an invalid Discover card number.

Source code in src/validators/card.py
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
@validator
def discover(value: str, /):
    """Return whether or not given value is a valid Discover card number.

    Examples:
        >>> discover('6011111111111117')
        # Output: True
        >>> discover('4242424242424242')
        # Output: ValidationError(func=discover, args={'value': '4242424242424242'})

    Args:
        value:
            Discover card number string to validate

    Returns:
        (Literal[True]): If `value` is a valid Discover card number.
        (ValidationError): If `value` is an invalid Discover card number.
    """
    pattern = re.compile(r"^(60|64|65)")
    return card_number(value) and len(value) == 16 and pattern.match(value)

validators.card.jcb(value)

Return whether or not given value is a valid JCB card number.

Examples:

>>> jcb('3566002020360505')
# Output: True
>>> jcb('4242424242424242')
# Output: ValidationError(func=jcb, args={'value': '4242424242424242'})

Parameters:

Name Type Description Default
value str

JCB card number string to validate

required

Returns:

Type Description
Literal[True]

If value is a valid JCB card number.

ValidationError

If value is an invalid JCB card number.

Source code in src/validators/card.py
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
@validator
def jcb(value: str, /):
    """Return whether or not given value is a valid JCB card number.

    Examples:
        >>> jcb('3566002020360505')
        # Output: True
        >>> jcb('4242424242424242')
        # Output: ValidationError(func=jcb, args={'value': '4242424242424242'})

    Args:
        value:
            JCB card number string to validate

    Returns:
        (Literal[True]): If `value` is a valid JCB card number.
        (ValidationError): If `value` is an invalid JCB card number.
    """
    pattern = re.compile(r"^35")
    return card_number(value) and len(value) == 16 and pattern.match(value)

validators.card.mastercard(value)

Return whether or not given value is a valid Mastercard card number.

Examples:

>>> mastercard('5555555555554444')
# Output: True
>>> mastercard('4242424242424242')
# Output: ValidationError(func=mastercard, args={'value': '4242424242424242'})

Parameters:

Name Type Description Default
value str

Mastercard card number string to validate

required

Returns:

Type Description
Literal[True]

If value is a valid Mastercard card number.

ValidationError

If value is an invalid Mastercard card number.

Source code in src/validators/card.py
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
@validator
def mastercard(value: str, /):
    """Return whether or not given value is a valid Mastercard card number.

    Examples:
        >>> mastercard('5555555555554444')
        # Output: True
        >>> mastercard('4242424242424242')
        # Output: ValidationError(func=mastercard, args={'value': '4242424242424242'})

    Args:
        value:
            Mastercard card number string to validate

    Returns:
        (Literal[True]): If `value` is a valid Mastercard card number.
        (ValidationError): If `value` is an invalid Mastercard card number.
    """
    pattern = re.compile(r"^(51|52|53|54|55|22|23|24|25|26|27)")
    return card_number(value) and len(value) == 16 and pattern.match(value)

validators.card.unionpay(value)

Return whether or not given value is a valid UnionPay card number.

Examples:

>>> unionpay('6200000000000005')
# Output: True
>>> unionpay('4242424242424242')
# Output: ValidationError(func=unionpay, args={'value': '4242424242424242'})

Parameters:

Name Type Description Default
value str

UnionPay card number string to validate

required

Returns:

Type Description
Literal[True]

If value is a valid UnionPay card number.

ValidationError

If value is an invalid UnionPay card number.

Source code in src/validators/card.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
@validator
def unionpay(value: str, /):
    """Return whether or not given value is a valid UnionPay card number.

    Examples:
        >>> unionpay('6200000000000005')
        # Output: True
        >>> unionpay('4242424242424242')
        # Output: ValidationError(func=unionpay, args={'value': '4242424242424242'})

    Args:
        value:
            UnionPay card number string to validate

    Returns:
        (Literal[True]): If `value` is a valid UnionPay card number.
        (ValidationError): If `value` is an invalid UnionPay card number.
    """
    pattern = re.compile(r"^62")
    return card_number(value) and len(value) == 16 and pattern.match(value)

validators.card.visa(value)

Return whether or not given value is a valid Visa card number.

Examples:

>>> visa('4242424242424242')
# Output: True
>>> visa('2223003122003222')
# Output: ValidationError(func=visa, args={'value': '2223003122003222'})

Parameters:

Name Type Description Default
value str

Visa card number string to validate

required

Returns:

Type Description
Literal[True]

If value is a valid Visa card number.

ValidationError

If value is an invalid Visa card number.

Source code in src/validators/card.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@validator
def visa(value: str, /):
    """Return whether or not given value is a valid Visa card number.

    Examples:
        >>> visa('4242424242424242')
        # Output: True
        >>> visa('2223003122003222')
        # Output: ValidationError(func=visa, args={'value': '2223003122003222'})

    Args:
        value:
            Visa card number string to validate

    Returns:
        (Literal[True]): If `value` is a valid Visa card number.
        (ValidationError): If `value` is an invalid Visa card number.
    """
    pattern = re.compile(r"^4")
    return card_number(value) and len(value) == 16 and pattern.match(value)