JS to determine IP address, subnet mask, gateway logical check

Source: Internet
Author: User
Tags bitwise

Because to do the static address configuration JS Check, find a lot of information found on the internet is about ip,mask validity check, no ip,submask,gateway logic judgment, write down the code for the needs of people reference.

Popularization of Gateway address knowledge:

is to proceed with the operation 1 with 1 with a 0 for 0,0 and 0 for 0. First, the IP and subnet masks are expanded 10.70.64.223        00001010.01000110.01000000.11011111255.255.255. 0   111111111.11111111.11111111.00000000 Network segment is         00001010.01000110.01000000.00000000 then converted to decimal is: 10.70.64.0 IP address and subnet mask do with the operation and the gateway address and subnet mask to do with the results of the operation obtained should be consistent, that is, the host number is consistent. I'm going to use JS to Ip,mask,gateway the "." After separation and judgment. JS Bitwise AND Operation: result = "Integer 1" & "Integer 1" & performs a bitwise AND operation on each digit of two 32-bit expression. If the two bits are all 1, the result is 1. Otherwise, the result is 0.

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 Legal
}
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; Legitimate
}
}   

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 (' 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 does not match subnet mask, gateway address! ‘);
return false;
}

JS to determine IP address, subnet mask, gateway logical check

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.