1. 检验IP
$nDotNum=0;//是否遇到第一个点
$nFirstSect=0; //第一段值
for($i=0;$i<strlen($text);$i++)
{
  $str = substr($text, $i, 1);//取一个字符,
  if( strcmp($str, ".") == 0 )//遇到点.
  {
     if( $nDotNum==0 )
     { //遇到第一个点
         if( $nFirstSect < 128 || $nFirstSect > 223 )
            return false; //不是B,C类
     }
     else{
        if( $nFirst > 255 )
          return false;  //其他部分不能大于255
     }
     $nDotNum++;
     $nFirstSect = 0;
  }
  else{//非点
    if( strcmp($str, "0") < 0 || strcmp($str,"9") > 0 )
         return false; //不是数字,返回false
    else{
         $nFirstSect = $nFirstSect * 10 + $str;
    }
  } 
  if( $nDotNum == 3 )
  {//以点结束的情况
     if( $i>= strlen($text) )
         return false;
  } 
}
  if( $nDotNum != 3 )
    return false;
return true;

解决方案 »

  1.   

    <form name=one>
      <input type=text name=ip size=30>
      <input type=button onclick="testIp()" value="Test"><Br>
      <!--问题二-->
    <input type=checkbox onclick="if(document.one.pwd.style.display=='none') document.one.pwd.style.display=''; else document.one.pwd.style.display='none'">验证密码
      <input type=password name=pwd size=30 value=hello style="display:none">
    </form>
    <!--问题一-->
    <script>
      function testIp()
      {
       ip=document.one.ip.value;
       first_byte=parseInt(ip.substr(0,ip.indexOf(".")));
       if((first_byte>128)&&(first_byte<223))
       alert("合法的B/C类地址");
       else
       alert("no");
       // 当然这只是看第一个字节,其它工作就没做了 
       // a.b.c.d 也算个ip??   :)
      }
    </script>
      

  2.   

    <form name=one>
      <input type=text name=ip size=30>
      <input type=button onclick="testIp()" value="Test"><Br>
      <!--Q2--><input type=checkbox onclick="if(document.one.pwd.style.display=='none') document.one.pwd.style.display=''; else document.one.pwd.style.display='none'">验证密码
      <input type=password name=pwd size=30 value=hello style="display:none">
    </form>
    <!--Q1-->
    <script>
      function testIp()
      {
       ip=document.one.ip.value;
       first_byte=parseInt(ip.substr(0,ip.indexOf(".")));
       if((first_byte>128)&&(first_byte<223))
       alert("合法的B/C类地址");
       else
       alert("no");  //更健壮的验证,自已加吧
      }
    </script>
      

  3.   

    请问document.one.pwd.style.display是需要支持JavaScript的浏览器吗?
    在Netscape+Linux+php下可以实现吗?