本代码是从个人项目中提取出来的,应该可以算的上是相当全面有效的IP相关信息的验证代码了,用来前端验证
10101100000100000000010011101011 */
var praseIpToBinary = function(ipAddress){
var numArray = ipAddress.split(".");
if(numArray.length != 4){
alert("输入的IP有误");
return;
}
var returnIpStr = "";
for (var i = 0; i < 4; i++) {
var curr_num = numArray[i];
var number_Bin = parseInt(curr_num);
number_Bin = number_Bin.toString(2);
var iCount = 8-number_Bin.length;
for (var j = 0; j < iCount; j++) {
number_Bin = "0"+number_Bin;
}
returnIpStr += number_Bin;
}
return returnIpStr;
}
/* 判断子网掩码是否合法 */
/* 子网掩码必须是 1 和 0组成的连续的一段 如 11110000 */
var judgesubnetMask = function(ipAddress){
var binaryIpString = praseIpToBinary(ipAddress).toString();
var subIndex = binaryIpString.lastIndexOf("1")+1;
var frontHalf = binaryIpString.substring(0,subIndex);
var backHalf = binaryIpString.substring(subIndex);
if(frontHalf.indexOf("0") != -1 || backHalf.indexOf("1") != -1){
return false;
}else{
return true;
}
}
/* 两个IP地址做 与 操作 返回结果 */
/* 该功能主要用来实现 IP地址和子网掩码 相与,获取当前IP地址的IP地址段 */
/* 以此来验证输入的网关地址是否合法 */
var getIPsAndResult = function(ipAddr1,ipAddr2){
var ipArray1 = ipAddr1.split(".");
var ipArray2 = ipAddr2.split(".");
var returnResult = "";
if(ipArray1.length != 4 || ipArray2.length != 4 ){
alert("输入的IP有误");
return;
}
for (var i = 0; i < 4; i++) {
var number1 = parseInt(ipArray1[i]);
var number2 = parseInt(ipArray2[i]);
returnResult += number1&number2;
if(i<3){
returnResult += ".";
}
}
return returnResult;
}
/* 判断网关地址是否合法 */
var judgeGatewayResult = function(ipAddr,subnetMask,gateway){
var andResult1 = getIPsAndResult(ipAddr,subnetMask);
var andResult2 = getIPsAndResult(gateway,subnetMask);
if(andResult1 == andResult2){
return true;
}else{
return false;
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。