diff --git a/scripts/pi-hole/js/groups-clients.js b/scripts/pi-hole/js/groups-clients.js index 49da0613..c883e5f5 100644 --- a/scripts/pi-hole/js/groups-clients.js +++ b/scripts/pi-hole/js/groups-clients.js @@ -257,22 +257,20 @@ function addClient() { if (ip.length === 0) { utils.enableAll(); - utils.showAlert("warning", "", "Warning", "Please specify a client IP address"); + utils.showAlert("warning", "", "Warning", "Please specify a client IP or MAC address"); return; } - // Validate IP address (may contain CIDR details) - var ipv6format = ip.includes(":"); + // Convert input to upper case (important for MAC addresses) + ip = ip.toUpperCase(); - if (!ipv6format && !utils.validateIPv4CIDR(ip)) { + // Validate input, can be: + // - IPv4 address (with and without CIDR) + // - IPv6 address (with and without CIDR) + // - MAC address (in the form AA:BB:CC:DD:EE:FF) + if (!utils.validateIPv4CIDR(ip) && !utils.validateIPv6CIDR(ip) && !utils.validateMAC(ip)) { utils.enableAll(); - utils.showAlert("warning", "", "Warning", "Invalid IPv4 address!"); - return; - } - - if (ipv6format && !utils.validateIPv6CIDR(ip)) { - utils.enableAll(); - utils.showAlert("warning", "", "Warning", "Invalid IPv6 address!"); + utils.showAlert("warning", "", "Warning", "Input is neither an IP nor a MAC address!"); return; } diff --git a/scripts/pi-hole/js/groups-common.js b/scripts/pi-hole/js/groups-common.js index b0156b0e..8283b0ad 100644 --- a/scripts/pi-hole/js/groups-common.js +++ b/scripts/pi-hole/js/groups-common.js @@ -129,6 +129,11 @@ function validateIPv6CIDR(ip) { return ipv6validator.test(ip); } +function validateMAC(mac) { + var macvalidator = new RegExp(/^([\dA-F]{2}:){5}([\dA-F]{2})$/); + return macvalidator.test(mac); +} + function bsSelect_defaults() { // set bootstrap-select defaults var pickerDEFAULTS = $.fn.selectpicker.Constructor.DEFAULTS; @@ -157,6 +162,7 @@ window.utils = (function () { enableAll: enableAll, validateIPv4CIDR: validateIPv4CIDR, validateIPv6CIDR: validateIPv6CIDR, + validateMAC: validateMAC, bsSelect_defaults: bsSelect_defaults }; })();