更改 keyup 上的输入字段以匹配 MAC 地址格式

Hit*_*ien 1 javascript jquery userscripts tampermonkey

我对 JavaScript 或 jQuery 没有太多经验。

我尝试使用 Tampermonkey 自动更正 MAC 地址的输入字段。

该站点需要一个格式为00:00:00:00:00:00.

所以我为 Tampermonkey 编写了这个脚本,以便在我输入时它会自动添加冒号:

// ==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();
});
Run Code Online (Sandbox Code Playgroud)
<input id=MAC />
Run Code Online (Sandbox Code Playgroud)

但它不起作用。输入框的 ID 是MAC

我哪里做错了,做错了多少?

解决方案

感谢 @i-wrestled-a-bear-once 和 @freginold 提供的最佳解决方案

我现在使用的是@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

});
Run Code Online (Sandbox Code Playgroud)

I w*_*ce. 5

  • replace(/[^\d|A-Z]/g, '') 删除任何非字母数字字符
  • match(/.{1,2}/g) 将字符串分成 2 个块
  • join(":") 连接块并在它们之间放置一个冒号

// ==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() { 
  // remove non digits, break it into chunks of 2 and join with a colon
  this.value = 
    (this.value.toUpperCase()
    .replace(/[^\d|A-Z]/g, '')
    .match(/.{1,2}/g) || [])
    .join(":")
});
Run Code Online (Sandbox Code Playgroud)
<input id=MAC />
Run Code Online (Sandbox Code Playgroud)