判断用户输入的是否为IP地址,IP地址,IP,IP提交

判断用户提交的是否为IP地址,看似简单,貌似就是字符串什么的对比,却也用了不少技术

我是用struts的validate判断的,只把validate方法粘贴上

public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request,HttpServletResponse response) {
  ActionErrors errors = new ActionErrors() ;
   //IP
   if(kk_BlacklistVehicle_UserEnrol_IP==null||kk_BlacklistVehicle_UserEnrol_IP.equals(""))
   {
    errors.add("errorKK_BlacklistVehicle_UserEnrol",new ActionMessage("kk_BlacklistVehicle_UserEnrol_IP.null")) ;
   }
   else
   {
    if(kk_BlacklistVehicle_UserEnrol_IP.length()>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() );
     boolean b = true;
     while( st.hasMoreElements() )
     {
      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;
       }
      }
     }
     boolean isIP = true;
     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++)
      {
       if(temp.charAt(i) == '.')
       {
        count++;
       }
      }
       if(count != 3)
       {
        isIP = false;
       }
     }
     if(!isIP)
     {
      //isIP为false,提示错误信息
      errors.add("errorKK_BlacklistVehicle_UserEnrol",new ActionMessage("kk_BlacklistVehicle_UserEnrol_IP.GeShi")) ;
     }
    }
   }
  return  errors;
 }

 PS:kk_BlacklistVehicle_UserEnrol_IP为用户提交上来的property
其他的我都加了详细的注释

粘贴个经我修改过的判断IP的带有main方法的类

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class ValidIP
{
 public static boolean isNumeric(String str)
 {
  StringTokenizer st = new StringTokenizer(str,".",false);
  System.out.println( "Token Total: " + st.countTokens() );
  boolean b = true;
  while( st.hasMoreElements() )
  {
   for (int i = st.nextToken().length();--i>=0;)
   {
    if (!(str.charAt(i) >= 48 && str.charAt(i) <= 57)) 
    {
     System.out.println("您输入的不是纯数字!请重新输入."); 
     b = false; 
     continue;
    } 
    else 
    { 
     b = true; 
    }
   }
  }
  return b;
 }

 public boolean validIP(String ip)
 {
  int count = 0;
  boolean isIP = true;
  String temp = ip;
  if(temp.charAt(0) == '.' || temp.charAt(temp.length()-1) == '.' || temp.length()>15)
  {
   isIP = false;
  }
  for(int i=0;i<temp.length();i++)
  {
   if(temp.charAt(i) == '.')
   {
    count++;
   }
  }
   if(count != 3)
   {
    isIP = false;
   }
  return isIP;
 }
 public static void main(String[] args) throws IOException
 {
//  System.out.print("Input IP Address:");
//  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//  String tempbr = new String(br.readLine());
//此处3行注释打开是由自己输入字符,我为了方便就把这里注释掉直接自己传字符串了
  ValidIP isip = new ValidIP();
  boolean a = isip.isNumeric("111.111.1.1.21");//判断传进去的字符串是否除了'.'就是数字
  System.out.println(a);
  if(a)//是的话进入这个if
  {
   boolean bl = isip.validIP("111.111.1.1.21");//判断这个ip地址的规格对不对
   System.out.println(bl);//对打印true错false
  }
 }
}

 下面再粘个用swing包的判断IP的类,有main方法,但我未修改,你自己修改一下吧,好象是这个输入英文字母也判断为正确的IP地址,用的话参考我上边的例子,用StringTokenizer控制一下,如果你有更好的方法,请留言,也让我学习学习

package test;

import javax.swing.JOptionPane;

public class IDTests
{
 public static void main(String as[])
 {
  String id = new String ();
  id=JOptionPane.showInputDialog(null,"Please input ID:");
  int add=id.length();
  String ad = new String();
  ad=String.valueOf(add);
  String test = new String();
  boolean b1=false;
  boolean b2=false;
  boolean b3=false;
  boolean b4=false;
 
  int found=1;
  if(id.charAt(0) == '.' || id.charAt(id.length()-1) == '.'|| add >15)
  {
   JOptionPane.showMessageDialog(null,"IPAddress is error");
  }
  else
  {
   for(int i=0;i<add;i++ )
   {
    if(String.valueOf(id.charAt(i)).equals("."))
    {
     //JOptionPane.showMessageDialog(null,String.valueOf(i));
     int testadd = Integer.parseInt(test);
     if(found==1)
     {
      if(testadd > 0 && testadd< 255)
      {
       b1=true;
      }
     }
     if(found==2)
     {
      if(testadd >=0 && testadd< 255)
      {
       b2=true;
      }
     }
     if(found==3)
     {
      if(testadd >=0 && testadd < 255)
      {
       b3=true;
      }
     }
     test=new String();
     found++;
    }
    else
    {
     test=test+String.valueOf(id.charAt(i));
     if(i==(add-1))
     {
      int testadd = Integer.parseInt(test);
      if(testadd >=0 && testadd< 255)
      {
       b4=true;
      }
  
     }
    }
   }
  }
  //JOptionPane.showMessageDialog(null,"b1+"+String.valueOf(b1));
  //JOptionPane.showMessageDialog(null,"b2"+String.valueOf(b2));
  //JOptionPane.showMessageDialog(null,"b3"+String.valueOf(b3));
  //JOptionPane.showMessageDialog(null,"b4"+String.valueOf(b4));
  if(b1&&b2&&b3&&b4)
  {
   JOptionPane.showMessageDialog(null,"IPAddress is exactly!");
  }
  else
  {
   JOptionPane.showMessageDialog(null,"IPAddress is error");
  }
 }
}

 

 

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值