javascript中判断IP地址格式
时间:2007-05-18 来源:freewildwolf
function checkIp(ip){
var ipPattern = new RegExp( "^(([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\\. ((0)|([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\\. ((0)|([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\\. ((0)|([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))$","g");
var ip6Pattern=new RegExp( "^(^::$) |(^([\\d|a-fA-F]{1,4}:){7}([\\d|a-fA-F]{1,4})$) |(^(::(([\\d|a-fA-F]{1,4}):){0,5}([\\d|a-fA-F]{1,4}))$) |(^(([\\d|a-fA-F]{1,4})(:|::)){0,6}([\\d|a-fA-F]{1,4})$)$","g"); if(ipPattern.test(ip)){
return true;
}
if (ip6Pattern.test(ip)){
var pos=ip.indexOf("::");
if (pos>-1)
{
pos=ip.indexOf("::",pos+2);
if (pos>-1) return false;
}
return true;
}
return false;
}
var ipPattern = new RegExp( "^(([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\\. ((0)|([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\\. ((0)|([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\\. ((0)|([1-9][0-9]?)|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))$","g");
var ip6Pattern=new RegExp( "^(^::$) |(^([\\d|a-fA-F]{1,4}:){7}([\\d|a-fA-F]{1,4})$) |(^(::(([\\d|a-fA-F]{1,4}):){0,5}([\\d|a-fA-F]{1,4}))$) |(^(([\\d|a-fA-F]{1,4})(:|::)){0,6}([\\d|a-fA-F]{1,4})$)$","g"); if(ipPattern.test(ip)){
return true;
}
if (ip6Pattern.test(ip)){
var pos=ip.indexOf("::");
if (pos>-1)
{
pos=ip.indexOf("::",pos+2);
if (pos>-1) return false;
}
return true;
}
return false;
}
相关阅读 更多 +