Javascript Determines whether two IP is in the same network segment instance code _javascript tips

Source: Internet
Author: User

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> &LT;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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.