js针对ip地址、子网掩码、网关的逻辑性判断
因为要做静态地址配置的js校验,找了好多资料发现网上都是关于ip,mask的有效性检查,没有ip,submask,gateway的逻辑性判断,自己写下代码供需要的人参考。
普及下网关地址知识:
第一点:进行与运算1与1得1,1与0为0,0与0为0。首先把ip和子网掩码展开
10.70.64.223 00001010.01000110.01000000.11011111
255.255.255。0 111111111.11111111.11111111.00000000
网段就是 00001010.01000110.01000000.00000000
然后转换成十进制就是:10.70.64.0
第二点:IP地址与子网掩码做与运算和网关地址与子网掩码做与运算得到的结果应该是一致的就对了,也就是主机号一致。
我这里是先用js将ip,mask,gateway按照‘.'分隔后相与做判断的。
第三点:js的按位与运算
result=【整数1】&【整数1】
&对两个32位表达式的每一个位执行按位“与”运算。如果两个位均为1,则结果是1。否则,结果为0。
分享js针对ip地址、子网掩码、网关的逻辑性判断详细代码
functioncheckIP(ip)
{
obj=ip;
varexp=/^(\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])$/;
varreg=obj.match(exp);
if(reg==null)
{
returnfalse;//不合法
}
else
{
returntrue;//合法
}
}
functioncheckMask(mask)
{
obj=mask;
varexp=/^(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)$/;
varreg=obj.match(exp);
if(reg==null)
{
returnfalse;//"非法"
}
else
{
returntrue;//"合法"
}
}
varstatic_ip=document.getElementById('static_ip').value;
varstatic_mask=document.getElementById('static_mask').value;
varstatic_gw=document.getElementById('static_gw').value;
if(static_ip=='')
{
//$("#static_ip_error").css("display","block");
document.getElementById('static_ip').focus();
returnfalse;
}elseif(!checkIP(static_ip))
{
//$("#static_ip_error").css("display","none");
document.getElementById('static_ip').focus();
returnfalse;
}
if(static_mask=='')
{
//$("#static_mask_error").css("display","block");
document.getElementById('static_mask').focus();
returnfalse;
}elseif(!checkMask(static_mask))
{
//$("#static_mask_error").css("display","none");
document.getElementById('static_mask').focus();
returnfalse;
}
if(static_gw=='')
{
//$("#static_gw_error").css("display","block");
document.getElementById('static_gw').focus();
returnfalse;
}elseif(!checkIP(static_gw))
{
//$("#static_gw_error").css("display","none");
document.getElementById('static_gw').focus();
returnfalse;
}
if(static_ip==static_mask||static_mask==static_gw||static_mask==static_gw)
{
alert('地址输入错误!');
returnfalse;//3个地址不能相同
}
varstatic_ip_arr=newArray;
varstatic_mask_arr=newArray;
varstatic_gw_arr=newArray;
static_ip_arr=static_ip.split(".");
static_mask_arr=static_mask.split(".");
static_gw_arr=static_gw.split(".");
varres0=parseInt(lan_ip_arr[0])&parseInt(static_mask_arr[0]);
varres1=parseInt(lan_ip_arr[1])&parseInt(static_mask_arr[1]);
varres2=parseInt(lan_ip_arr[2])&parseInt(static_mask_arr[2]);
varres3=parseInt(lan_ip_arr[3])&parseInt(static_mask_arr[3]);
varres0_gw=parseInt(static_gw_arr[0])&parseInt(static_mask_arr[0]);
varres1_gw=parseInt(static_gw_arr[1])&parseInt(static_mask_arr[1]);
varres2_gw=parseInt(static_gw_arr[2])&parseInt(static_mask_arr[2]);
varres3_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地址与子网掩码、网关地址不匹配!');
returnfalse;
}
js验证IP及子网掩码的合法性代码分享:
functioncheckIP(ip)
{
obj=ip;
varexp=/^(\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])$/;
varreg=obj.match(exp);
if(reg==null)
{
returnfalse;//不合法
}
else
{
returntrue;//合法
}
}
functioncheckMask(mask)
{
obj=mask;
varexp=/^(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)$/;
varreg=obj.match(exp);
if(reg==null)
{
returnfalse;//"非法"
}
else
{
returntrue;//"合法"
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助。