Skip to content

mac_address

validators.mac_address.mac_address(value)

Return whether or not given value is a valid MAC address.

This validator is based on WTForms MacAddress validator.

Examples:

>>> mac_address('01:23:45:67:ab:CD')
# Output: True
>>> mac_address('00:00:00:00:00')
# Output: ValidationError(func=mac_address, args={'value': '00:00:00:00:00'})

Parameters:

Name Type Description Default
value str

MAC address string to validate.

required

Returns:

Type Description
Literal[True]

If value is a valid MAC address.

ValidationError

If value is an invalid MAC address.

New in version 0.2.0.

Source code in /opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/validators/mac_address.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
@validator
def mac_address(value: str, /):
    """Return whether or not given value is a valid MAC address.

    This validator is based on [WTForms MacAddress validator][1].

    [1]: https://github.com/wtforms/wtforms/blob/master/src/wtforms/validators.py#L482

    Examples:
        >>> mac_address('01:23:45:67:ab:CD')
        # Output: True
        >>> mac_address('00:00:00:00:00')
        # Output: ValidationError(func=mac_address, args={'value': '00:00:00:00:00'})

    Args:
        value:
            MAC address string to validate.

    Returns:
        (Literal[True]):
            If `value` is a valid MAC address.
        (ValidationError):
            If `value` is an invalid MAC address.

    > *New in version 0.2.0*.
    """
    return re.match(r"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$", value) if value else False