Skip to content

Validate MAC Address with JS & regex

Simple JavaScript function with some regex used to validate a MAC Address format. Used this on ServiceNow instances and before that as a PowerShell function on Microsoft environments.

function isMacValid(mac) {
    var regexMac = /^((([0-9A-F]{2}:){5})|(([0-9A-F]{2}-){5})|([0-9A-F]{10}))([0-9A-F]{2})$/i
    var testRegex = mac.match(regexMac) >= 0;
    // Returns true if it's a incorrect format
    if (testRegex) {
        return true;
    // Returns false if it's a correct format
    } else if (!testRegex) {
        return false;
    }
}

Example of usage (wrong mac format)

isMacValid('1a:2b:3c:4d:5e6f');

Example of usage (correct mac format)

isMacValid('1a:2b:3c:4d:5e:6f');
Published inDevelopment

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2020 Tech[Net] | Crafted with ♥ by Morten & rollin' on Author