JS _javascript techniques for logical judgment of IP address, subnet mask and gateway

Source: Internet
Author: User
Tags bitwise

Because to do static address configuration JS Check, find a lot of data found online are about ip,mask validity check, no ip,submask,gateway logical judgment, write down the code for the needs of the people reference.

Popularization of Gateway address knowledge:

1th: Proceed with the operation 1 with 1 1,1 with 0 for 0,0 and 0 for 0. First, expand the IP and subnet masks
10.70.64.223 00001010.01000110.01000000.11011111
255.255.255. 0 111111111.11111111.11111111.00000000
Network segment is 00001010.01000110.01000000.00000000
Then convert to decimal: 10.70.64.0

2nd: IP address and subnet mask do and operation and gateway address and subnet mask to do and operation of the results should be the same, that is, the host number is consistent.
I am here to use JS to Ip,mask,gateway according to '. ' Separating and making judgments.

3rd:The bitwise AND operation of JS

result = "Integer 1" & "Integer 1"
& performs a bitwise AND operation on each digit of two 32-bit expressions. If the two digits are 1, the result is 1. Otherwise, the result is 0.

Share JS logical judgment for IP address, subnet mask, gateway , detailed code

function Checkip (IP) {obj=ip; var exp=/^ (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. 
 (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) $/; 
 var reg = Obj.match (exp);  
 if (reg==null) {return false;//not valid} else {return true;//valid}} function Checkmask (mask) {obj=mask; var exp=/^ (254|252|248|240|224|192|128|0) \.0\.0\.0|255\. (254|252|248|240|224|192|128|0) \.0\.0|255\.255\. (254|252|248|240|224|192|128|0) \.0|255\.255\.255\. 
 (254|252|248|240|224|192|128|0) $/; 
 var reg = Obj.match (exp); if (Reg==null) {return false;//"illegal"} else {return true;//"legitimate"} var static_ip= document.geteleme
  Ntbyid (' static_ip '). Value; 
  var static_mask= document.getelementbyid (' Static_mask '). Value;

  
  var static_gw= document.getelementbyid (' STATIC_GW '). Value;
   if (static_ip== ') {//$ ("#static_ip_error"). CSS ("Display", "block");
   document.getElementById (' Static_ip '). focus ();
  return false; }else if (!checkip(STATIC_IP))
   {//$ ("#static_ip_error"). CSS ("display", "none");
   document.getElementById (' Static_ip '). focus ();    
  return false;
   } if (static_mask== ') {//$ ("#static_mask_error"). CSS ("Display", "block");
   document.getElementById (' Static_mask '). focus ();  
  return false; 
   }else if (!checkmask (Static_mask)) {//$ ("#static_mask_error"). CSS ("display", "none");
   document.getElementById (' Static_mask '). focus ();  
  return false;
   } if (static_gw== ') {//$ ("#static_gw_error"). CSS ("Display", "block");
   document.getElementById (' STATIC_GW '). focus ();  
  return false;
   }else if (!checkip (STATIC_GW)) {//$ ("#static_gw_error"). CSS ("display", "none");
   document.getElementById (' STATIC_GW '). focus ();    
  return false; } if (static_ip = Static_mask | | static_mask = = STATIC_GW | | static_mask = = STATIC_GW) {alert (' Address input Error!)
  '); return false;
 3 addresses cannot be the same} var static_ip_arr = new Array;
 var static_mask_arr = new Array; var Static_gw_arr = new Array;
 Static_ip_arr = Static_ip.split (".");
 Static_mask_arr = Static_mask.split (".");

 Static_gw_arr = Static_gw.split (".");
 var Res0 = parseint (Lan_ip_arr[0]) & parseint (static_mask_arr[0));
 var res1 = parseint (lan_ip_arr[1]) & parseint (static_mask_arr[1));
 var res2 = parseint (lan_ip_arr[2]) & parseint (static_mask_arr[2));
 
 var res3 = parseint (Lan_ip_arr[3]) & parseint (static_mask_arr[3));
 var RES0_GW = parseint (Static_gw_arr[0]) & parseint (static_mask_arr[0));
 var RES1_GW = parseint (static_gw_arr[1]) & parseint (static_mask_arr[1));
 var RES2_GW = parseint (static_gw_arr[2]) & parseint (static_mask_arr[2));
 
 var RES3_GW = parseint (Static_gw_arr[3]) & parseint (static_mask_arr[3)); if (RES0==RES0_GW && res1==res1_gw && RES2==RES2_GW && RES3==RES3_GW) {}else{alert (' IP address and child Network mask, gateway address does not match!
  ');
 return false;
 }

JS authentication IP and subnet mask legitimate code sharing:

function Checkip (IP) 
{ 
  obj=ip;
  var exp=/^ (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) $/; 
  var reg = Obj.match (exp); 
  if (reg==null) 
  {return 
    false;//not valid
  } 
  else 
  {return 
    true;//Valid
  } 
}
 
function Checkmask (mask) 
{ 
  obj=mask; 
  var exp=/^ (254|252|248|240|224|192|128|0) \.0\.0\.0|255\. (254|252|248|240|224|192|128|0) \.0\.0|255\.255\. (254|252|248|240|224|192|128|0) \.0|255\.255\.255\. (254|252|248|240|224|192|128|0) $/; 
  var reg = Obj.match (exp); 
  if (reg==null) 
  {return 
     false;//' Illegal '
  } 
   else 
  {return 
     true;//' Legal '
  } 
}

The above is the entire content of this article, I hope to help you learn.

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.