Logical judgment of js for ip address subnet mask gateway

  • 2020-11-18 06:06:29
  • OfStack

Because of the need to do static address configuration js verification, I found a lot of data on the Internet are about ip, mask validity check, ip, submask, gateway logical judgment, write their own code for reference.

Popularize gateway address knowledge:

Point 1: Perform and operation 1 and 1 get 1,1 and 0 are 0,0 and 0 are 0. First, expand the ip and subnet mask
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 base 10:10.70.64.0

Point 2: The IP address and subnet mask do and operation and the gateway address and subnet mask do and operation should be 1, that is, host number 1.
Here, I first use js to separate ip, mask and gateway according to '.' and then make judgment.

Point 3: Bitwise and operation of js

result = [integer 1] & [Integer 1]
& Performs a bitwise "and" operation on each bit of two 32-bit expressions. If both bits are 1, the result is 1. Otherwise, the result is 0.

Share js's detailed code for logical determination of ip addresses, subnet masks, and gateways


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;// illegal 
 } 
 else 
 { 
  return true; // legal 
 } 
}
 
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 "
 } 
} 
 var static_ip= document.getElementById('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(' Error in address typing! ');
  return false; //3 The 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 The address does not match the subnet mask and gateway address! ');
  return false;
 }

js Validates the validity of IP and subnet mask 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;// illegal 
  } 
  else 
  { 
    return true; // legal 
  } 
}
 
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 "
  } 
}

Above is the entire content of this article, I hope to help you with your study.


Related articles: