js針對ip地址、子網(wǎng)掩碼、網(wǎng)關(guān)的邏輯性判斷
因為要做靜態(tài)地址配置的js校驗,找了好多資料發(fā)現(xiàn)網(wǎng)上都是關(guān)于ip,mask的有效性檢查,沒有ip,submask,gateway的邏輯性判斷,自己寫下代碼供需要的人參考。
普及下網(wǎng)關(guān)地址知識:
第一點:進行與運算1與1得1,1與0為0,0與0為0。首先把ip和子網(wǎng)掩碼展開
10.70.64.223 00001010 .01000110.01000000.11011111
255.255.255。0 111111111.11111111.11111111.00000000
網(wǎng)段就是 00001010 .01000110.01000000.00000000
然后轉(zhuǎn)換成十進制就是:10.70.64.0
第二點:IP地址與子網(wǎng)掩碼做與運算和網(wǎng)關(guān)地址與子網(wǎng)掩碼做與運算得到的結(jié)果應(yīng)該是一致的就對了,也就是主機號一致。
我這里是先用js將ip,mask,gateway按照‘.'分隔后相與做判斷的。
第三點:js的按位與運算
result = 【整數(shù)1】 & 【整數(shù)1】
& 對兩個 32 位表達式的每一個位執(zhí)行按位“與”運算。 如果兩個位均為 1,則結(jié)果是 1。 否則,結(jié)果為 0。
分享js針對ip地址、子網(wǎng)掩碼、網(wǎng)關(guān)的邏輯性判斷詳細代碼
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地址與子網(wǎng)掩碼、網(wǎng)關(guān)地址不匹配!');
return false;
}
js驗證IP及子網(wǎng)掩碼的合法性代碼分享:
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; //"合法"
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
- Shell腳本獲取本地網(wǎng)卡IP、mac地址、子網(wǎng)掩碼、dns IP、外網(wǎng)IP
- C++實現(xiàn)獲取IP、子網(wǎng)掩碼、網(wǎng)關(guān)、DNS等本機網(wǎng)絡(luò)參數(shù)的方法
- Python實現(xiàn)根據(jù)IP地址和子網(wǎng)掩碼算出網(wǎng)段的方法
- C#設(shè)置本地網(wǎng)絡(luò)如DNS、網(wǎng)關(guān)、子網(wǎng)掩碼、IP等等
- JS 根據(jù)子網(wǎng)掩碼,網(wǎng)關(guān)計算出所有IP地址范圍示例
- 網(wǎng)絡(luò)編程基礎(chǔ)(局域網(wǎng)、ip、子網(wǎng)掩碼、網(wǎng)關(guān)、DNS)概念理解
相關(guān)文章
Popup彈出框添加數(shù)據(jù)實現(xiàn)方法
這篇文章主要為大家詳細介紹了Popup彈出框添加數(shù)據(jù)的簡單實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
在Chrome DevTools中調(diào)試JavaScript的實現(xiàn)
這篇文章主要介紹了在Chrome DevTools中調(diào)試JavaScript的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

