日期:2014-05-16  浏览次数:20304 次

请教各位,这个函数应该如何修改才能更加完善?

function macFormCheck(mac) {
    var macs = new Array();
    macs = mac.split("_"); 
    if(macs.length != 6){ 
     alert("The MAC you input is invalid!Please input a mac like xx_xx_xx_xx_xx_xx (xx should be sexadecimal number)! ");
isOK = 0 ;
     return null ;
  }
    for (var s=0; s<6; s++) { 
     var temp = parseInt(macs[s],16);  
     if(macs[s].length > 2 ||macs[s].length < 2  )  
{
         alert("The MAC you input is invalid!Please input a mac like xx_xx_xx_xx_xx_xx (xx should be sexadecimal number)! ");
isOK = 0 ;
     return null ;
}
     if(isNaN(temp))  
{
         alert("The MAC you input is invalid!Please input a mac like xx_xx_xx_xx_xx_xx (xx should be sexadecimal number)! ");
isOK = 0 ;
     return null;
}
     if(temp < 0 || temp > 255 )  
{
         alert("The MAC you input is invalid!Please input a mac like xx_xx_xx_xx_xx_xx (xx should be sexadecimal number)! ");
isOK = 0 ;
     return null ;
}
    }
  isOK = 1 ;
    return mac;
}


代码如上,我是想做一个检查输入的MAC地址合法性的函数,但是判断功能不是很完善。
请教各位应该如何修改。

------解决方案--------------------
你是觉得哪里不完善呢
------解决方案--------------------

function macFormCheck(mac) {
    mac = mac + '';
    if(/^[A-F\d]{2}(_[A-F\d]{2}){5}$/.test(mac))return mac;
    alert("The MAC you input is invalid!Please input a mac like xx_xx_xx_xx_xx_xx (xx should be sexadecimal number)!");
    return null
};
var ms = ['00_0F_12_AF_FF_FE', 'ab_0F_12_AF_FF_FE', '001_0F_12_AF_FF_FE'];
for(var i = ms.length - 1; i >= 0; i--)alert(ms[i] + '\r\n' + macFormCheck(ms[i]))

------解决方案--------------------
/^(?:[\da-f]{2,2}-){7,7}[\da-f]{2,2}$/i