I am trying to validate an IP address using core javascript. I would like to make sure that each IP address section value must be greater>100. But it is not working properly. Please find the following code for more details.
function testIP(){
var ip=document.getElementById("ipaddress").value;
var first= ip.substring(0,3);
var second= ip.substring(4,7);
var third= ip.substring(8,11);
var fourth= ip.substring(12,15);
var error=false;
if(first){
var ippart1 = 0+first;
if(first<100){
}
else{
alert("Please enter a valid ip address.First part of ip address is incorrect");
error=true;
}
}
else
error=true;
if(second){
var ippart2 = 0+second;
if(ippart2<100){
}
else{
alert("Please enter a valid ip address.Second part of ip address is incorrect");
error=true;
}
}
else
error=true;
if(third){
var ippart3 = 0+third;
if(ippart3<100){
}
else{
alert("Please enter a valid ip address.Third part of ip address is incorrect");
error=true;
}
}
else
error=true;
if(fourth){
var ippart4 = 0+fourth;
if(ippart4<100){
}
else{
alert("Please enter a valid ip address.Forth part of ip address is incorrect");
error=true;
}
}
else
error=true;
if(error==true){
return false;
}
else
return true;
}
I think the problem is in the string to integer conversion. I have also tried parseInt
function. It is also not working. Please take a look at it.