关于输入一个有意义的数值,而不是数字的判断,该怎么做?
例如,人数01或001应该是错误的,金额00.1也是错误的,如何写个方法进行检查?只能用java在服务器端判定,不能用javascript。谢谢!

解决方案 »

  1.   

    就JSP被。数字判断的逻辑为什么是 001不对啊? 
    如果楼主坚持这样的逻辑就判断字符串呗。
      

  2.   

    转化成int或者float然后判断大小嘛
      

  3.   

    不用javascript,那用自定义标签了
      

  4.   

    我觉得如果你说的01,001,00.1如果能通过的话,那你设的一定是String类型的了,那就用equals()判断String val1="01";String val2="001";System.out.println(val1.equals(val2));
      

  5.   

    或者你限制一下输入的长度,同样也可以在servlet里做,
    value1=request.getparamer("TEST");
    if(value1.length!=1)
    {
      String errors="There is some wrong in here.";
      Super.getInfo().setValue("errors",errors);
      return "JSP";
    }
      

  6.   

    用正则表达式是解决这一类问题的一个方法,例如你说的01和001的问题就可以说是数字首位必须是1-9之间的数字。
    public static boolean isNum(String str){
    String formatEmail = "([1-9])[0-9]*$";
    return str.matches(formatEmail);
    } public static void main(String[] args) throws Exception{
    String sNum = "01";
    System.out.println(isNum(sNum));
    }其他的你自己写就是了,这样灵活性比较大,什么email,指定小数精度之类的都可以做到
      

  7.   

    import java.util.regex.*;
    public static boolean isData(String str)
      {
        String regex = "^(-|\\+)?\\d+\\.?\\d*$";
        //String regex="[0-9]*[.]?[0-9]*";
        String regex1 = "^(-|\\+)?0+\\d+\\.?\\d*$";
        Pattern ptn = Pattern.compile(regex);
        Matcher mat = ptn.matcher(str);
        boolean result = mat.matches();
        System.out.println(">>> first result = " + result);
        if (1 > 2)
        {
          ptn = Pattern.compile(regex1);
          mat = ptn.matcher(str);
          System.out.println(">>> second result = " + mat.matches());
          result = !mat.matches();
        }
        mat = null;
        ptn = null;
        System.out.println(str + " is data ? " + result);
        return result;
      }
      

  8.   

    //上面有点错  if (1 > 2)import java.util.regex.*;public static boolean isData(String str)
      {
        String regex = "^(-|\\+)?\\d+\\.?\\d*$";
        //String regex="[0-9]*[.]?[0-9]*";
        String regex1 = "^(-|\\+)?0+\\d+\\.?\\d*$";
        Pattern ptn = Pattern.compile(regex);
        Matcher mat = ptn.matcher(str);
        boolean result = mat.matches();
        System.out.println(">>> first result = " + result);
        if (result)
        {
          ptn = Pattern.compile(regex1);
          mat = ptn.matcher(str);
          System.out.println(">>> second result = " + mat.matches());
          result = !mat.matches();
        }
        mat = null;
        ptn = null;
        System.out.println(str + " is data ? " + result);
        return result;
      }