自己再稍微改动改动,没用的我都给注释掉了:// JavaScript Document
/*******************************
*Model:Devices,IPAM
*Info:Input IP Text 's Validity
*Date:2007-11-12
*Author:Yuming
*******************************/var matchIp=/^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/;
function check_Ip(str)
{
if(!str.match(matchIp))
{
return 1;
}/*
else if(str=="0.0.0.0" || str=="255.255.255.255")
{
return 1;
}
else if(str.substr(0,str.indexOf("."))=="0")
{
return 1;
}
else if(str.substr(0,str.indexOf("."))=="00")
{
return 1;
}
else if(str.substr(0,str.indexOf("."))=="000")
{
return 1;
}*/
return 0;}
/*******************************
*Model:Devices,IPAM
*Info:Input IP Text 's Validity
*Date:2007-11-12
*Author:Yuming
*******************************/var matchIp=/^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/;
function check_Ip(str)
{
if(!str.match(matchIp))
{
return 1;
}/*
else if(str=="0.0.0.0" || str=="255.255.255.255")
{
return 1;
}
else if(str.substr(0,str.indexOf("."))=="0")
{
return 1;
}
else if(str.substr(0,str.indexOf("."))=="00")
{
return 1;
}
else if(str.substr(0,str.indexOf("."))=="000")
{
return 1;
}*/
return 0;}
if(check_Ip(IPAddress.value)}
{
alert("ip address is ok");
}
<script language="javascript">
function isIP(strIP)
{
var re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g //匹配IP地址的正则表达式
if(re.test(strIP))
{
if( RegExp.$1 >0&&RegExp.$1 <256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4>0&&RegExp.$4<256)
{
alert("正确");
return true;
}
document.all.xx.value="";
alert("错误");
return false;
}
}
</script><input name="xx" onblur="isIP(this.value)">
楼上的回复都只能检查如 192.168.1.7 这种。要求:
输入内容可以是域名,如 news.163.com, www.tp-link.com, www.99read.com
可以是单独的IP, 如 172.16.3.54
可以是IP/MASK, 如 192.168.1.0/255.255.255.0
可以是IP/掩码位数, 如 10.10.0.0/16
上述的几种输入都是有效的。
function checkform(frm)
{
var ip1=/^(\d+)\.(\d+)\.(\d+)\.(\d+)\/(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
var ip2=/^(\d+)\.(\d+)\.(\d+)\.(\d+)\/(\d+)$/;
var ip3=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
var str = frm.address.value;
alert(str);
if(ip1.test(str) && RegExp.$1 >0 && RegExp.$1<256 && RegExp.$2 <256 && RegExp.$3 <256 && RegExp.$4 <256
&& RegExp.$5<256 && RegExp.$6<256 && RegExp.$7 < 256 && RegExp.$8 < 256)
{
alert("ok1");
return 1;
}
if(ip2.test(str) && RegExp.$1 >0 && RegExp.$1<=224 && RegExp.$2 <256 && RegExp.$3 <256 && RegExp.$4 <256
&& RegExp.$5 >0 && RegExp.$5 < 32)
{
alert("ok2");
return 1;
}
if(ip3.test(str) && RegExp.$1 >0 && RegExp.$1<=224 && RegExp.$2 <256 && RegExp.$3 <256 && RegExp.$4 <256)
{
alert("ok3");
return 1;
}
alert("error");
return 0;
}
</script>
<input type=text id=ip name=ip value="127.0.0.1">
<input type=button onclick=check_ip(document.getElementById("ip").value) value="检测">
<script>
function check_ip(ip){
var reg=/^(([01]?[\d]{1,2})|(2[0-4][\d])|(25[0-5]))(\.(([01]?[\d]{1,2})|(2[0-4][\d])|(25[0-5]))){3}$/
if(reg.test(ip)){
alert("IP格式正确!");
}else{
alert("IP格式错误!");
}
}
</script>
这个怎么样啊
<script language="javascript">
function isIP(strIP) {
var re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g
var rew= /^[w]{3}\.[\w-]{1,}\.(com|cn|net)$/;
if(re.test(strIP)){
if( RegExp.$1 >0&&RegExp.$1 <256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4>0&&RegExp.$4<256) {
alert("正确");
return true;
}
}
if(rew.test(strIP)){
alert("正确");
return true;
}
document.all.xx.value="";
alert("错误");
return false;
}
</script>
<input name="xx" onblur="isIP(this.value)">