欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

Mac地址验证的javascript代码_javascript技巧

程序员文章站 2024-01-05 08:42:16
...
验证规则:

1. Broadcast mac address (FF:FF:FF:FF::FF:FF:)

2. IPv4 & IPv6 multicast mac address

3. Numeric character

code list :

复制代码 代码如下:

function isValidMacAddress(address) {
var c = '';
var i = 0, j = 0;

if ((address.toLowerCase() == 'ff:ff:ff:ff:ff:ff') || (address.toLowerCase() == '00:00:00:00:00:00')) {
alert('error');
return false;
}

var addrParts = address.split(':');
if (addrParts.length != 6) {
alert('error');
return false;
}
for (i = 0; i if (addrParts[i] == ''){
alert('error');
return false;
}
}

if (addrParts[i].length != 2) {
alert('error');
return false;
}
for (j = 0; j c = addrParts[i].toLowerCase().charAt(j);
if ((c >= '0' && c = 'a' && c continue;
} else {
alert('error');
return false;
}
}

if ((parseInt(addrParts[0], 16) % 2) == 1) {
alert('error');
return false;
}

return true;
}