Javascript 判斷兩個IP是否在同一網段執行個體代碼_javascript技巧

來源:互聯網
上載者:User

Javascript 判斷兩個IP是否在同一網段

以下指令碼不做IP格式的判斷,只判斷兩個IP是否在同一網段。

例子,Javascript判斷兩個IP是否在同一網段。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML>  <HEAD>  <TITLE>   IP段資訊檢測_www.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("不在同一網段");       return false;;      }     }    }    alert("在同一網段");    return true;   }  </script> </HEAD>  <BODY>  <input name="Ip1" id="Ip1" maxlength=15>  <br>  <input name="Ip2" id="Ip2" maxlength=15>  <br>  <input name="netmask" id="netmask" maxlength=15>  <input type="button" value="計算" onClick="checkSameNetMask( document.getElementById('Ip1').value , document.getElementById('Ip2').value , document.getElementById('netmask').value ); "> </BODY></HTML>

需要的朋友可以拿去參考。感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.