方法1,
复制代码 代码示例:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>检查是否为
IP地址-www.jb200.com</title>
<script language="
javascript">
function check(){
str = formdata.txt.value;
str = str.match(/(d+).(d+).(d+).(d+)/g);
if (str == null){
alert("输入不能为空");
return false;
}else if (RegExp.$1>255 || RegExp.$2>255 || RegExp.$3>255 || RegExp.$4>255){
alert("你输入的IP地址无效");
return false;
}else{
alert("你输入的IP地址有效");
return true;
}
}
</script>
</head>
<body>
<form name="formdata" onSubmit="return check()">
<input type="text" name="txt">
<input type="submit" value="检验">
</form>
</body>
</html>
方法2,
复制代码 代码示例:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> 检查是否为IP地址-www.jb200.com</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</HEAD>
<BODY>
<title>IP地址合法性验证js代码 - </title>
请输入IP地址:<input type="text" id="ip">
<input type="button" onclick="checkIP()" value="验证">
</BODY>
</HTML>
<script language="javascript">
function checkIP()
{
obj = document.getElementById("ip").value;
//ip地址
var exp=/^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$/;
var reg = obj.match(exp);
if(reg==null)
{
alert("IP地址不合法!");
}
else
{
alert("IP地址合法!");
}
}
</script>