DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> Javascript 判斷兩個IP是否在同一網段實例代碼
Javascript 判斷兩個IP是否在同一網段實例代碼
編輯:關於JavaScript     

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>

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

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved