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


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?+