JS 正则校验 MAC 地址,以破折号分割,正则不太好,这里记录一下
function checkMacAddress( macAddress ) { | |
var regex = "([A-Fa-f0-9]{2}-){5}[A-Fa-f0-9]{2}"; | |
//var regex = "(([A-Fa-f0-9]{2}-){5}[A-Fa-f0-9]{2})|(([A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2})"; // 含冒号 | |
var regexp = new RegExp(regex); | |
if (!regexp.test(macAddress)) { | |
console.log("Mac地址格式不正确,请检查") | |
return false; | |
} | |
return true; | |
} |
附:JS 替换字符串中所有符合条件的字符,如替换所有的冒号(全角,半角)为破折号,并转小写
var regSingleByte = new RegExp(":","g"); // 半角冒号匹配全部 | |
var regDoubleByte = new RegExp(":","g"); // 全角冒号匹配全部 | |
var mac = macDom.val().toLowerCase().replace(regSingleByte,"-").replace(regDoubleByte,"-"); | |
macDom.val(mac); |