IP地址验证,IP验证,费了好大劲才写好的,顶下把``截串,截取字符串StringTokenizer

   

 if(kk_BlacklistVehicle_UserEnrol_IP.length()>15)
    {//判断输入的大于15位的直接出错
     errors.add("errorKK_BlacklistVehicle_UserEnrol",new ActionMessage("kk_BlacklistVehicle_UserEnrol_IP.leng")) ;
    }
    else
    {
     StringTokenizer st = new StringTokenizer(kk_BlacklistVehicle_UserEnrol_IP,".",false);//去掉IP地址中的'.'
     System.out.println( "Token Total: " + st.countTokens() );//分出了几个字符串
     if(st.countTokens()!=4)
     {//判断截取出来的字符串如果不是4段直接出错
      errors.add("errorKK_BlacklistVehicle_UserEnrol",new ActionMessage("kk_BlacklistVehicle_UserEnrol_IP.GeShi")) ;
     }
     boolean b = true;
     int cou = 1 ;
     while( st.hasMoreElements() )
     {
      cou++;
      for (int i = st.nextToken().length();--i>=0;)
      {
       //判断去掉'.'后其他字符是否都是数字
       if (!(kk_BlacklistVehicle_UserEnrol_IP.charAt(i) >= 48 && kk_BlacklistVehicle_UserEnrol_IP.charAt(i) <= 57)) 
       {//判断除了数字还有其他字符直接出错
        System.out.println("您输入的IP中除了数字和.还包括了其他字符!请重新输入.");
        errors.add("errorKK_BlacklistVehicle_UserEnrol",new ActionMessage("kk_BlacklistVehicle_UserEnrol_IP.ZiFu")) ;
        b = false;
        break;
       }
       else
       {
        b = true;
       }
      }
     }
     
      
     StringTokenizer str = new StringTokenizer(kk_BlacklistVehicle_UserEnrol_IP,".",false);//去掉IP地址中的'.'
     System.out.println( "Token Total: " + str.countTokens() );
     while( str.hasMoreElements() )
     {
      cou++;
      str.nextToken();
     }
     while( str.hasMoreElements() )
     {
      if(b)
      {
       for (int i = 1;i<cou;i++)
       {
        String token = str.nextToken() ;
        int stSize = Integer.parseInt(token);
        System.out.println(token);
 
        if (stSize>255||stSize<0)
        {
         System.out.println("您输入的IP段不在0~255之间!请重新输入");
         errors.add("errorKK_BlacklistVehicle_UserEnrol",new ActionMessage("kk_BlacklistVehicle_UserEnrol_IP.Size")) ;
         b = false;
         break;
        }
        else
        {
         b = true;
        }
       }
      }
      else
      {
       break ;
      }
     }
     boolean isIP = true;
     int j = 0 ;
     if(b)
     {
      //去掉'.'后其他字符是否都是数字进入到此if
      int count = 0;
      String temp = kk_BlacklistVehicle_UserEnrol_IP;
      if(temp.charAt(0) == '.' || temp.charAt(temp.length()-1) == '.' || temp.length()>15)
      {
       //判断第一位和最后一位是不是'.',是的话isIp设置为false
       isIP = false;
      }
      //这个for循环判断'.'是不是3个,如果不是3个,设置isIP为fasle
      for(int i=0;i<temp.length();i++)
      {
       j++ ;
       if(temp.charAt(i) == '.')
       {
        count++;
        j = 0 ;
        if(temp.charAt(i)==temp.charAt(i+1))
        {
         count--;
        }
       }
       if(temp.charAt(i) != '.')
       {
        //判断数字是不是多于3位了,比3位多就count--
        if(j>3)
        {
         count--;
        }
       }
       if(temp.charAt(i) == '0'&&(temp.charAt(i+1)=='1'||temp.charAt(i+1)=='2'||temp.charAt(i+1)=='3'||temp.charAt(i+1)=='4'||temp.charAt(i+1)=='5'||temp.charAt(i+1)=='6'||temp.charAt(i+1)=='7'||temp.charAt(i+1)=='8'||temp.charAt(i+1)=='9'))
       {
        //判断IP地址是否为这种形式,01.32.043.34,出现01,043这种格式不正确
        count--;
       }
      }
       if(count != 3)
       {
        isIP = false;
       }
     }
     if(!isIP)
     {
      //isIP为false,提示错误信息
      errors.add("errorKK_BlacklistVehicle_UserEnrol",new ActionMessage("kk_BlacklistVehicle_UserEnrol_IP.GeShi")) ;
     }
    }

 

黑色头发  http://heisetoufa.iteye.com

如果发现本文有误,欢迎批评指正

你可能感兴趣的:(J#)