0

I am trying to validate input from a textfield so that it only contains characters from a-z. I am using the following method to validate if the input is an 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;
    }
}

How can I achieve this using a String? I know there is a different way of doing this using an if statement but I was wondering if I can catch an exception like in the example above.

Thanks

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140

3 Answers3

1

You can use matches with regex so if you want to check your input is an int or not you can use:

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

If you want to check if the input contain only alphabetic, you can use :

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

If you want to check if your input contains alphanumeric you can use :

if(userInput.matches("[a-zA-Z0-9]+")){
    System.out.println("correct");
}else{
    System.out.println("Not correct");
} 
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
1

Use regex:

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

If you want to allow uppercase too:

if (!userInput.matches("[a-zA-Z]+"))
    // Has characters other than a-z or A-Z
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

You could use something like:

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

Alternative solution to @Bohemian♦ to allow uppercase:

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

A similar method, according to your source:

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;
}

There your question is, if it's possible to throw/catch an exception, you could do the following:

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;
    }

Considering that you'll need a class:

class MyException extends Exception {

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

}

An abstract solution would be:

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

Throw it as:

 throw new MyException ("Something happened")

Catch as:

catch (MyException e)
{
   // Do something
}

For more info, check this for regex.

xMarcoGP
  • 5
  • 7