入力ボックスに正当なIPアドレスがあるかどうかを検出します.


1.第一の方法:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>     IP  </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> New Document </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}|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)
{
alert("IP     !");
}
else
{
alert("IP    !");
}
}
</script>