Because I need to do js verification of static address configuration, I searched a lot of information and found that the Internet is all about the validity check of ip and mask. There is no logical judgment of ip, submask and gateway. I wrote the code myself for those who need it. refer to.
Popular knowledge about gateway addresses:
First point: Perform the AND operation of 1 and 1 to get 1, 1 and 0 are 0, and 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
The network segment is 00001010 .01000110.01000000.00000000
Then converted to decimal it is: 10.70.64.0
Second point: The AND operation of the IP address and the subnet mask and the AND operation of the gateway address and the subnet mask should be the same. , that is, the host numbers are consistent.
Here, I first use js to separate ip, mask, and gateway according to ‘.’ and then combine them to make a judgment.
The third point: 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.
Sharejs logical judgment on ip address, subnet mask, gatewayDetailed 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;//不合法 } else { return true; //合法 } } 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; //"非法" } else { return true; //"合法" } } 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('地址输入错误!'); return false; //3个地址不能相同 } 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地址与子网掩码、网关地址不匹配!'); return false; }
JS verification code for legality of IP and subnet mask 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;//不合法 } else { return true; //合法 } } 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; //"非法" } else { return true; //"合法" } }
The above is the entire content of this article, I hope it will be helpful to everyone’s study.