通过表单验证整数值

时间:2015-10-20 11:34:34

标签: java forms integer validating

如何通过java中的表单验证整数值

if(!(Integer.parseInt(txtStart.getText()).equals(txtStart.getText()))
{
    JOptionPane.showMessageDialog(null, "Please Enter Valid Start Page");
}

1 个答案:

答案 0 :(得分:0)

使用下一个代码,您可以验证您的文本是否为数字。

public class Main {

    public static void main(String args[]){
        String txtValue1 = "";
        String txtValue2 = "a";
        String txtValue3 = "3";

        System.out.println("txtValue1: "+isNumber(txtValue1));
        System.out.println("txtValue2: "+isNumber(txtValue2));
        System.out.println("txtValue3: "+isNumber(txtValue3));
    }

    private static boolean isNumber(String _value){
        boolean result = true;

        try{
            Double.parseDouble(_value); 
        }catch(Exception e){
            result = false;
        }
        return result;
    }

}

输出

txtValue1: false
txtValue2: false
txtValue3: true