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

2
  • 2
    Possible duplicate of Check if String contains only letters Commented May 20, 2017 at 14:22
  • You can throw your own exception. Using an if statement is more performant than try catch Commented May 20, 2017 at 14:24

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

Comments

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

8 Comments

The integers was only an example
i don't know @TimBiegeleisen this is sad in the first second dv
what did you mean @cricket_007 ?
I understand that the OP want to validate if an input is an Integer of not @cricket_007 if i'm wrong explain to me !
@YCF_L actually, the OP is contradicting. First says "contains characters from a-z" and then "method to validate if the input is an int". Not your fault. You're correct in your interpretation of it.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.