How do I validate the format of a MAC address?

前端 未结 11 1423
逝去的感伤
逝去的感伤 2020-12-15 05:59

What\'s the best way to validate that an MAC address entered by the user?

The format is HH:HH:HH:HH:HH:HH, where each H is a hexadecimal ch

11条回答
  •  时光说笑
    2020-12-15 06:57

    If you want to ensure that there is either '-' or ':' throughout but not both, you can use following in Python:

    import re
    
    def is_valid_macaddr802(value):
        allowed = re.compile(r"""
                             (
                                 ^([0-9A-F]{2}[-]){5}([0-9A-F]{2})$
                                |^([0-9A-F]{2}[:]){5}([0-9A-F]{2})$
                             )
                             """,
                             re.VERBOSE|re.IGNORECASE)
    
        if allowed.match(value) is None:
            return False
        else:
            return True
    

提交回复
热议问题