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地址合法性的函数,但是判断功能不是很完善。
请教各位应该如何修改。

解决方案 »

  1.   


    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]))
      

  2.   

    如果字母可以是小写,把正则表达式/^[A-F\d]{2}(_[A-F\d]{2}){5}$/改为:
    /^[A-F\d]{2}(_[A-F\d]{2}){5}$/i
      

  3.   

    函数的使用我是这么使用的function macFormCheck() {
       with ( document.forms[0] ) { 
         var mac = MAC.value + '';
         if(/^[A-Fd]{2}(_[A-Fd]{2}){5}$/i.test(mac))return true; 
         alert("The MAC you input is invalid!Please input a mac like xx_xx_xx_xx_xx_xx (xx should be sexadecimal number)!"); 
         return false ;
      }
    }
    为什么我输入一个11_22_33_44_55_55,却总是走到提示那一步呢?
      

  4.   

    正则错了。
    /^[A-F\d]{2}(_[A-F\d]{2}){5}$/i
    注意d前面的\,估计是转义字符引起错误
      

  5.   

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