This code is extracted from personal projects and should be able to calculate a fairly comprehensive and effective authentication code for IP-related information, used for front-end validation
* ******************/* To determine whether the IP address is legal/var judgeipislegal = function (ipaddr) {var regips =/^ ((25[0-5]|2[0-4]\d|1\d {2}| [1-9]\d| [0-9]) \.) {3} (25[0-5]|2[0-4]\d|1\d{2}| [1-9]\d| [0-9]))
$/;
Return Regips.test (IPADDR); }/* IP address converted to binary string/////////////////////(172.16.4.235--> 10101100000100000000010011101011/var praseiptobinary
SS) {var numarray = Ipaddress.split (".");
if (numarray.length!= 4) {alert ("Input IP error");
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; }/* To determine if the subnet mask is legitimate///The subnet mask must be a contiguous section of 1 and 0 such as 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; }/* Two IP addresses to do and operation return/////* This feature is mainly used to implement IP address and subnet mask phase, get the IP address of the current IP addresses section * * To verify the input gateway address is legitimate/var Getipsandresult = funct
Ion (IPADDR1,IPADDR2) {var ipArray1 = Ipaddr1.split (".");
var ipArray2 = Ipaddr2.split (".");
var returnresult = "";
if (iparray1.length!= 4 | | | | iparray2.length!= 4) {alert ("Incorrect IP input");
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; }/* Determine if the gateway address is legitimate/var Judgegatewayresult = function (Ipaddr,subnetmask,gateway) {var andResult1 = Getipsandresult (ipaddr
, SubnetMask);
var andResult2 = Getipsandresult (Gateway,subnetmask); if (andResult1 = = andResult2) {return true;
}else{return false;
}
}
The above is the entire contents of this article, I hope you can enjoy.