Javascript 无法找到问题:将iplib js用于常规ip配置函数,无效ip返回有效ip
你能帮我找到哪里我错了,因为我是新的javascript。 我正在尝试将iplib js用于与ip配置相关的常见函数。问题是,当我尝试验证无效ip时,其验证函数返回true 图书馆: 请试一试这款plunker: 我追溯了正在使用的函数->这些块: 分析后发现其返回0表示无效字符,因此跳过验证条件:Javascript 无法找到问题:将iplib js用于常规ip配置函数,无效ip返回有效ip,javascript,validation,Javascript,Validation,你能帮我找到哪里我错了,因为我是新的javascript。 我正在尝试将iplib js用于与ip配置相关的常见函数。问题是,当我尝试验证无效ip时,其验证函数返回true 图书馆: 请试一试这款plunker: 我追溯了正在使用的函数->这些块: 分析后发现其返回0表示无效字符,因此跳过验证条件: var IPv4 = NetworkAddress.extend({ constructor:function(address) { this.base(address,".");
var IPv4 = NetworkAddress.extend({
constructor:function(address) {
this.base(address,".");
},
validate:function() {
/*
* Returns true if the normalized IP address is a valid IPv4 address
*/
var res = true;
this.iter(function(p) {
if(p < 0 || p > 255) {
res = false;
return false;
}
});
//console.log(res);
return res;
},
iterbits:function(callback) {
/*
* Iterate over each bit of the IP address and runs the callback
* Also passes the index to the callback as well
*/
var b = this.toBin();
for(var i = 0; i < b.length; i++) {
if(callback(parseInt(b[i]),i) === false) break;
}
},
normalize:function() {
/*
* Uses the raw IP address and converts it into a standard IPv4 address
*/
var p = this._address.split(this._separator);
while(p.length < IPv4.PARTS) p.push("0");
for(var i = 0; i < p.length; i++) {
while(p[i].length < 3) p[i] = "0"+p[i];
}
return p.join(this._separator);
},
此外,还使用以下方法来解析此块:
iter:function(callback,radix) {
/*
* Provides a way to iterate over the parts of the IP address.
* Passes the each part to the callback, and the index.
* Use radix to specify which output format you require (see parts for
*/
var p = this.parts(radix);
for(var i = 0; i < p.length; i++) {
if(callback(p[i],i) === false) break;
}
},
parts:function(radix) {
if(!radix) radix = 10;
var p = this.normalize().split(this._separator);
for(var i = 0; i < p.length; i++) {
p[i] = parseInt(p[i]);
if(radix!==10) p[i] = p[i].toString(radix);
}
return p;
return this.normalize().split(this._separator);
},
这是我的HTML:
<script>
function myFunction() {
var ip = new IPv4("a.x.d");
document.write(ip);
document.write("<br>");
var ip_norm = ip.normalize(); //"192.168.000.100"
document.write(ip_norm);
document.write("<br>");
var checkip = ip.validate();
document.write(checkip);
}
</script>
</head>
<body onload="myFunction()">
<div>testing</div>
</body>
我不想碰这个库中的任何东西,所以使用这个临时解决方案
parts:function(radix) {
if(!radix){
console.log("setting redix 10");
radix = 10;
}
// ***added bad fix here***
// ***only enter when redix 10 (my scenarioes)
if(radix === 10){
var p = this.normalize();
var ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
if(p.match(ipformat))
{
console.log("******** VALID IP ************");
}
else
{
console.log("********** INVALID IP ***********");
// if invalid make everything 256.256.256.256 so that it get caught in original validate
var p = this.normalize().split(this._separator);
for(var i = 0; i < p.length; i++) {
p[i] = 256;
}
return p;
}
}
// this is normal routine to be followed for error checking if redix is not 10 ,or valid ip (skipped from regx used)
var p = this.normalize().split(this._separator);
for(var i = 0; i < p.length; i++) {
console.log("inside converter part = " +i);
console.log("before parsing");
console.log(p[i]);
p[i] = parseInt(p[i]);
console.log("after parsing");
console.log(p[i]);
if(radix!==10)
{ console.log("only if redix not 10");
p[i] = p[i].toString(radix);
console.log(p[i]);
}
}
console.log("returning p");
console.log(p);
return p;
return this.normalize().split(this._separator);
}
在进一步的挖掘过程中,在部分:functionradix.So的parseInt中发现了将00d转换为0的过程,在validatevalue>255和value中