Javascript Determines whether two IP is on the same network segment
The following script does not judge the IP format and only determines whether two IP is in the same network segment.
Example, JavaScript determines whether two IP is on the same network segment.
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" > <HTML> <HEAD> <TITLE> IP segment Information detection _WW w.manongjc.com </TITLE> <script language= "JavaScript" type= "Text/javascript" > Function checksamenetmask (Value1, value2, netmask)
{var ip1 = new Array ();
var ip2 = new Array ();
var nm = new Array ();
IP1 = Value1.split (".");
IP2 = Value2.split (".");
NM = Netmask.split (".");
var ip1_2s = "";
var ip2_2s = "";
var inm2s = "";
var index = 0;
for (index = 0; index < 4; index++) {var ip_1 = new Array ();
var ip_2 = new Array ();
var n_m = new Array ();
Ip_1 = parseint (Ip1[index]). toString (2);
ip_2 = parseint (Ip2[index]). toString (2);
N_m = parseint (Nm[index]). toString (2);
var Tindex;
for (tindex = 0; Tindex < (8-ip_1.length); tindex++) {ip1_2s = "0";
} ip1_2s + = Ip_1; for (tindex = 0; Tindex < (8-ip_2.length); tindex++) {ip2_2s + + "0";
} ip2_2s + = ip_2;
for (tindex = 0; Tindex < (8-n_m.length); tindex++) {inm2s = "0";
} inm2s + = N_m;
var len = inm2s.length;
var Ip_12 = new Array ();
var ip_22 = new Array ();
var n_m_2 = new Array ();
Ip_12 = Ip1_2s.split ("");
ip_22 = Ip2_2s.split ("");
N_m_2 = Inm2s.split ("");
for (index = 0; index < len; index++) {if (n_m_2[index] = = "1") {if (Ip_12[index]!= Ip_22[index]) {
Alert ("Not on the same network segment");
return false;;;
} alert ("On the same network segment");
return true; } </script> </HEAD> <BODY> <input name= "Ip1" id= "Ip1" maxlength=15> <br> <INP UT name= "Ip2" id= "Ip2" maxlength=15> <br> <input name= "netmask" id= "netmask" maxlength=15> <input Type= "button" value= "Calculates" onclick= "Checksamenetmask (document.getElementById (' Ip1 '). Value, document.getElementById ( ' Ip2 '). Value, document.getElementById (' netmask '). Value );
"> </BODY> </HTML>
Friends You need can refer to them. Thank you for reading, I hope to help you, thank you for your support for this site!