【问题标题】:Validate Input - String验证输入 - 字符串
【发布时间】:2017-05-20 14:20:46
【问题描述】:

我正在尝试验证来自文本字段的输入,以便它只包含 a-z 中的字符。我正在使用以下方法来验证输入是否为 int:

//VALIDATE IF INPUT IS NUMERIC.
public static boolean isInt(JFXTextField txtUserInput, String userInput) {
    try {
        int intValidation = Integer.parseInt(userInput);
        txtUserInput.setStyle("-fx-border-color: ; -fx-border-width: 0px ;");
        return true;
    } catch(NumberFormatException exception) {
        showErrorMsg("Invalid Input:", "Please enter a numeric-only value");
        txtUserInput.setStyle("-fx-border-color: RED; -fx-border-width: 1px ;");
        return false;
    }
}

如何使用字符串来实现这一点?我知道使用 if 语句可以使用不同的方法来执行此操作,但我想知道是否可以像上面的示例中那样捕获异常。

谢谢

【问题讨论】:

标签: java string validation


【解决方案1】:

使用正则表达式:

if (!userInput.matches("[a-z]+"))
    // Has characters other than a-z

如果你也想允许大写:

if (!userInput.matches("[a-zA-Z]+"))
    // Has characters other than a-z or A-Z

【讨论】:

    【解决方案2】:

    您可以将matches 与正则表达式一起使用,因此如果您想检查您的输入是否为 int,您可以使用:

    String userInput = ...;
    if(userInput.matches("\\d+")){
        System.out.println("correct");
    }else{
        System.out.println("Not correct");
    }
    

    如果要检查输入是否仅包含字母,可以使用:

    if(userInput.matches("[a-zA-Z]+")){
        System.out.println("correct");
    }else{
        System.out.println("Not correct");
    }
    

    如果您想检查您的输入是否包含字母数字,您可以使用:

    if(userInput.matches("[a-zA-Z0-9]+")){
        System.out.println("correct");
    }else{
        System.out.println("Not correct");
    } 
    

    【讨论】:

    • 整数只是一个例子
    • 我不知道@TimBiegeleisen 这在第一秒 dv 中很悲伤
    • @cricket_007 是什么意思?
    • 我知道如果我错了,OP 想要验证输入是否是不是@cricket_007 的整数!
    • @YCF_L 实际上,OP 是矛盾的。首先说“包含 a-z 中的字符”,然后是“验证输入是否为 int 的方法”。不是你的错。你对它的解释是正确的。
    【解决方案3】:

    你可以使用类似的东西:

    if (!userInput.matches(".*[^a-z].*")) { 
        // Do something
    }
    

    @Bohemian♦ 允许大写的替代解决方案:

    if (!userInput.toLowerCase().matches(".*[^a-z].*")) { 
        // Do something
    }
    

    类似的方法,根据您的来源:

    public static boolean containsAZ(JFXTextField txtUserInput) {
            if (!txtUserInput.getText().toLowerCase().matches(".*[^a-z].*"))
                return true;
            else
                System.err.println("Input is not containing chars between A-Z");
    
            return false;
    }
    

    您的问题是,如果可以抛出/捕获异常,您可以执行以下操作:

    public static boolean containsAZ(JFXTextField txtUserInput) {
            try {
                if (!txtUserInput.toLowerCase().matches(".*[^a-z].*")) {
                    return true;
                } else
                    throw new MyException("Something happened");
            } catch (MyException e) {
                e.printStackTrace();
            }
            return false;
        }
    

    考虑到你需要一个类:

    class MyException extends Exception {
    
        public MyException(String e) {
            System.out.println(e);
        }
    
    }
    

    一个抽象的解决方案是:

    public class MyException extends Exception {
            // special exception code goes here
    }
    

    把它当作:

     throw new MyException ("Something happened")
    

    捕捉为:

    catch (MyException e)
    {
       // Do something
    }
    

    欲了解更多信息,check this for regex

    【讨论】:

      猜你喜欢
      • 2016-06-12
      • 2012-05-10
      • 1970-01-01
      • 2011-12-17
      • 2015-04-29
      • 1970-01-01
      • 2010-09-28
      • 2016-04-26
      • 1970-01-01
      相关资源
      最近更新 更多