求验证IP地址的JS代码。带注释。谢谢~~

JavaScript030

求验证IP地址的JS代码。带注释。谢谢~~,第1张

<SCRIPT language=JScript event=OnObjectReady(objObject,objAsyncContext) for=foo>

if(objObject.IPEnabled != null &&objObject.IPEnabled != "undefined" &&objObject.IPEnabled == true)

{

if(objObject.MACAddress != null &&objObject.MACAddress != "undefined")

MACAddr = objObject.MACAddress

if(objObject.IPEnabled &&objObject.IPAddress(0) != null &&objObject.IPAddress(0) != "undefined")

IPAddr = objObject.IPAddress(0)

if(objObject.DNSHostName != null &&objObject.DNSHostName != "undefined")

sDNSName = objObject.DNSHostName

}

</SCRIPT>

你想问的是ipv4和ipv6吧?

ipv4地址中间是用点(.)隔开,比如192.168.0.1

ipv6地址中间使用冒号(:)隔开,比如80:0:0:0:0:0:0:1

用脚本判断就很简单了,有多种方法,

var ip

if(ip.indexOf(".")>0){

//ipv4地址

}else{

//ipv6地址

}

/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/