I don't have much experience with JavaScript or jQuery.
I tried to use Tampermonkey to auto correct the input field for a MAC address.
The site wants a MAC address formatted as 00:00:00:00:00:00
.
So I wrote this script for Tampermonkey so it automatically adds the colons while I'm typing:
// ==UserScript==
// @name Name
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Adds colons to the mac adress of the Mac Field
// @author You
// @match Somesite
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
document.getElementById("MAC").addEventListener('keyup', function() {
var mac = document.getElementById('MAC').value;
var macs = mac.split('');
var colons = ["2", "5", "8", "11", "14"];
for (var col in colons) {
if (macs[col] == "-") {
macs[col] = ":";
} else if (macs[col] != "") {
} else if (macs[col] != ":") {
var colo = col + 1;
macs[colo] = macs[col];
macs[col] = ":";
}
}
mac = macs.toString();
});
<input id=MAC />
But it don't work. The ID of the inputfield is MAC
.
Where and how much did I do wrong?
SOLUTION
Thanks to @i-wrestled-a-bear-once and @freginold for the , my oppion, best solutions
I'm using now a slightly changed version from @freginold
var back = true;
function isHex(char) {
// check if char is a hex char
if (!isNaN(parseInt(char))) {
return true;
} else {
switch (char.toLowerCase()) {
case "a":
case "b":
case "c":
case "d":
case "e":
case "f":
return true;
}
return false;
}
}
document.getElementById("MAC").addEventListener('keydown', function() {
var key = event.keyCode || event.charCode;
if( key == 8 || key == 46 ) {
back = false;
}
});
document.getElementById("MAC").addEventListener('keyup', function() {
var key = event.keyCode || event.charCode;
var mac = document.getElementById('MAC').value;
var newMac = mac.replace("-", ""); // remove any dashes
if ((isHex(mac[mac.length - 1]) && (isHex(mac[mac.length - 2])) && (mac.length <= 16) && (back))) {
// if last two chars are numbers, insert a colon
newMac = newMac + ":";
}
back = true;
document.getElementById('MAC').value = newMac; // put new value into input field
});
You could simplify it and check if the last two characters in the string are hex characters (0-9, A-F) and if so, insert a :
. You can also use .replace()
to remove any occurrence of -
if you (or someone else) types a dash instead of a colon.
That way you can cover inserting colons if you don't type them at all, as well as converting any typed dashes to colons.
Here's a working example:
// ==UserScript==
// @name Name
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Adds colons to the mac adress of the Mac Field
// @author You
// @match Somesite
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
function isHex(char) {
if (!isNaN(parseInt(char))) {
return true;
} else {
switch (char.toLowerCase()) {
case "a":
case "b":
case "c":
case "d":
case "e":
case "f":
return true;
break;
}
return false;
}
}
document.getElementById("MAC").addEventListener('keyup', function() {
var mac = document.getElementById('MAC').value;
if (mac.length < 2) {
return;
}
var newMac = mac.replace("-", "");
if ((isHex(mac[mac.length - 1]) && (isHex(mac[mac.length - 2])))) {
newMac = newMac + ":";
}
document.getElementById('MAC').value = newMac;
});
document.getElementById('MAC').focus(); // autofocus for testing
<input id=MAC />