我想验证使用核心JavaScript的IP地址。我想确保每个IP地址部分的值必须大于100。但它不能正常工作。请查找以下代码以获取更多详细信息。IP地址验证不起作用
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;
}
我觉得问题是在字符串到整数的转换。我也试过parseInt
函数。它也没有工作。请看看它。
也许你可以使用一些正则表达式? http://stackoverflow.com/a/5284410/1331430 –
究竟是什么问题?在提问时,说出你期望发生的事情以及发生的事情非常重要。 – Pointy
你遇到什么错误? –