Mac Address Regex Javascript Validator

Search...

⌘K

Mac Address Regex Javascript Validator

Search...

⌘K


Mac Address Regex Javascript Validator

Mac Address Regex Javascript Validator

Easily validate MAC addresses in your JavaScript projects using our MAC Address Regex JavaScript Validator. Designed for developers managing networks, device identification, and configuration tasks, this tool ensures MAC address inputs follow the correct format. Pair it with the JavaScript Regex Tester to experiment with custom patterns, or try the IP Address Regex JavaScript Validator for validating related network data. For frontend applications, use it alongside the Password Regex JavaScript Validator to secure user data with strict input checks.

bb:aa:dd:aa:55:55
Possible security issues
This regex appears to be safe.
Explanation
  • [A-Z]: uppercase letters
  • [a-z]: lowercase letters
  • [0-9]: digits
  • \.: a literal dot
  • +: one or more of the preceding
  • *: zero or more of the preceding
  • ?: optional (zero or one)
  • ^: start of string
  • $: end of string
Match information
Match 1: "bb:aa:dd:aa:55:55" at index 0
Test your APIs today!

Write in plain English — Qodex turns it into secure, ready-to-run tests.

Regular Expression - Documentation

What is MAC Address Regex?


A MAC (Media Access Control) address is a unique identifier assigned to network interfaces. It typically appears in the format 00:1A:2B:3C:4D:5E or 00-1A-2B-3C-4D-5E, using hexadecimal digits separated by colons or hyphens.


In JavaScript, we can use regular expressions (regex) to verify whether a string follows this structure before using it in networking, device identification, or access filtering.


MAC Address Regex Pattern


The commonly used regex to validate MAC addresses is:

^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})


This matches:

  • Six groups of two hexadecimal characters

  • Separated by either : or -

  • Case-insensitive (thanks to [A-Fa-f])


How to Validate MAC Address using Regex in JavaScript


Here’s a full JavaScript example:


function isValidMacAddress(mac) {
  const macRegex = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
  return macRegex.test(mac);
}

console.log(isValidMacAddress("00:1A:2B:3C:4D:5E")); // true
console.log(isValidMacAddress("00-1A-2B-3C-4D-5E")); // true
console.log(isValidMacAddress("001A.2B3C.4D5E"));    // false


Validating MAC Addresses in Python


Prefer working in Python? You can use regular expressions there too for MAC address validation. Here’s a concise, developer-friendly approach:

import re

def is_valid_mac(mac):
    """
    Checks if the provided string is a valid MAC address
    (supports colon, hyphen, and Cisco dot notation)
    """
    mac_regex = re.compile(
        r"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"   # Matches 00:1A:2B:3C:4D:5E or 00-1A-2B-3C-4D-5E
        r"^([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4}$"      # Matches Cisco format like 001A.2B3C.4D5E
    )
    return bool(mac_regex.match(mac.strip()))

# Example usage:
test_cases = [
    "01-23-45-67-89-AB",      # True
    "01:23:45:67:89:AB",      # True
    "0123.4567.89AB",         # True
    "01-23-45-67-89-AH",      # False (H is not hex)
    "01-23-45-67-AH",         # False (missing groups)
]

for mac in test_cases:
    print(f"{mac}: {is_valid_mac(mac)}")


Validating MAC Addresses with Regex in Java


If you're working in Java and need to ensure your MAC addresses are formatted correctly, regular expressions offer a reliable solution—just like in JavaScript. The approach is very similar: define the regex, compile it, and check your target string.

Here's a concise example:

import java.util.regex.Pattern;

public class MacAddressValidator {
    // Regex pattern accepts both colon, hyphen, and dot-separated formats
    private static final Pattern MAC_REGEX = Pattern.compile(
        "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$" +
        "^([0-9A-Fa-f]{4}\\.[0-9A-Fa-f]{4}\\.[0-9A-Fa-f]{4})$"
    );

    public static boolean isValidMac(String input) {
        return input != null && MAC_REGEX.matcher(input).matches();
    }
}

Example Usage

Quickly test your implementation with real-world samples:

System.out.println(isValidMac("01-23-45-67-89-AB")); // true
System.out.println(isValidMac("01:23:45:67:89:AB")); // true
System.out.println(isValidMac("0123.4567.89AB"));    // true
System.out.println(isValidMac("01-23-45-67-89-AH")); // false
System.out.println(isValidMac("01-23-45-67-AH"));    // false


How it works:

  • The regex matches:

    • 6 groups of two hexadecimal characters, separated by colons or hyphens (e.g., AB:CD:EF:01:23:45)

    • Or, Cisco-style dot notation (ABCD.EF01.2345)

  • Both uppercase and lowercase letters are accepted.

This lets you use Java for robust MAC address input validation, whether you're building server-side tools or network utilities.


Validating MAC Addresses in C# with Regular Expressions


If you need to validate MAC addresses outside of JavaScript—say, in a C# backend or desktop app—the process is quite similar. Regular expressions in C# work just as well for pattern matching and input validation.

Here’s how you can check whether a string is a valid MAC address format in C#:

using System.Text.RegularExpressions;

bool IsValidMacAddress(string input)
{
    // Regex matches formats like 01-23-45-67-89-AB, 01:23:45:67:89:AB, or 0123.4567.89AB
    var macRegex = new Regex(
        @"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$^([0-9A-Fa-f]{4}\.){2}[0-9A-Fa-f]{4}$"
    );
    return !string.IsNullOrEmpty(input) && macRegex.IsMatch(input

Example Usage

Console.WriteLine(IsValidMacAddress("01-23-45-67-89-AB")); // true
Console.WriteLine(IsValidMacAddress("01:23:45:67:89:AB")); // true
Console.WriteLine(IsValidMacAddress("0123.4567.89AB"));    // true
Console.WriteLine(IsValidMacAddress("01-23-45-67-89-AH")); // false
Console.WriteLine(IsValidMacAddress("01-23-45-67-AH"));    // false

This function considers both common MAC address formats (colon-separated, hyphen-separated, or dot-separated). It’s perfect for validating user input, updating network settings, or filtering device lists in any .NET-based application.

MAC Address Validation with Regex in C++


If you're working in C++ and need to perform MAC address validation, regular expressions (regex) provide an efficient solution—similar in principle to JavaScript, just with a few different libraries.

Here's a simple C++ function that checks whether a given string is a valid MAC address:

#include 
#include 
#include 

// Returns true if 'mac' matches common MAC address formats.
Bool isValidMacAddress(const std::string& mac) {
    std::regex macPattern(
        "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"     // Standard: 01:23:45:67:89:AB or 01-23-45-67-89-AB
        "^([0-9A-Fa-f]{4}\\.[0-9A-Fa-f]{4}\\.[0-9A-Fa-f]{4})$" // Cisco-like: 0123.4567.89AB
    );
    return std::regex_match(mac, macPattern);
}

Example Usage

Here's how you can use this function in a basic C++ app:

#include 
int main() {
    std::vector testMacs = {
        "01-23-45-67-89-AB",
        "01:23:45:67:89:AB",
        "0123.4567.89AB",
        "01-23-45-67-89-AH", // Invalid: ‘H’ isn’t hexadecimal
        "01-23-45-67-AH"     // Invalid: not enough groups
    };

    for (const auto& mac : testMacs) {
        std::cout << mac << " => " << (isValidMacAddress(mac) ? "Valid" : "Invalid") << std::endl;
    }
}

This setup will print whether each test MAC address is valid or not, following the typical formats used in networking.

With this approach, you can ensure your C++ network tools, device management scripts, or backend services reliably filter out malformed MAC addresses before further processing.


Time and Space Complexity of MAC Address Regex Validation


When you check if a MAC address matches the regular expression, the efficiency matters—especially if you’re processing network data at scale. The regex engine examines each character in the input string once, making the validation process linear with respect to input length. So, for a MAC address of length N, the operation runs in O(N) time.

As for space, no matter the size of the input, regex validation in JavaScript uses a fixed amount of additional memory. There’s no need to allocate space that scales with your input—so the space complexity stays at O(1). This means your app remains lightweight, even when handling lots of addresses.


Real-World Use Cases


  • Network configuration: Validate MAC addresses before storing or using them in router or server config tools.

  • Device management: Identify and verify devices in IoT ecosystems.

  • Access control: Allow or block specific MACs in security-sensitive systems.

  • Form input validation: Make sure users don’t enter invalid MAC formats in web applications.


Pro Tips


  • Always trim whitespace from input strings before validation.

  • Regex is for format checking, not legitimacy. A syntactically valid MAC address may still not exist.

  • Support both : and - formats if your application handles different sources.

  • Consider using Base64 Encoder for secure storage or transmission.

  • Use this in combination with Token Generator to assign unique device tokens post-validation.


JavaScript Metacharacters Used


  • ^ : Anchors the regex at the start of the string.

  • $ : Anchors the regex at the end of the string.

  • [0-9A-Fa-f] : Matches a single hexadecimal character (case-insensitive).

  • {2} : Quantifier – exactly 2 characters.

  • [:-] : Matches either ':' or '-'.

  • {5} : Quantifier – matches the group 5 times.

  • (…) : Capturing group.


Example Regex Inputs


  1. "01:23:45:67:89:AB" → Valid

  2. "01-23-45-67-89-AB" → Valid

  3. "0123.4567.89AB" → Invalid

  4. "G1:23:45:67:89:ZZ" → Invalid


Combine with These Tools


Use this MAC Address Validator alongside:


Frequently asked questions

Can I use this regex to validate MAC addresses with dots (.) like Cisco formats?×
No. This validator only supports colon : and hyphen - formats. You’ll need a custom pattern for Cisco formats.
Is MAC validation case-sensitive?+
Can this regex detect real MAC addresses?+
Should I validate MACs on the client or server?+
What happens if I enter extra colons or hyphens?+