Show Error Message if a String contains a numeric character

0

Well that's my question, I'm doing a form in java swing and when filling in the fields of the JTextField I want to launch an error window in case it contains some numeric character. I have been testing many things but I have not reached anything. I am currently caught here:

        if (labor.isEditable()) { //Este if es porque dependiendo del usuario
        try { //que introduzca, habrá casillas en las que pueda escribir o no
            Integer.parseInt(labor.getText().toString()); //En el caso de que esto lance una excepción significara que la conversión no se ha realizado y por tanto es un String
            JOptionPane.showMessageDialog(null, "La labor NO PUEDE tener contenido numerico");
            labor.setText(""); //Pongo en blanco el campo del JTextField
            valido = false;

        } catch (NumberFormatException e) {

            valido = true; //Verifico de que el contenido es correcto

        }
    }

I've been doing tests and there are times that if I detect it and it shows me the error message in these cases:

  • If I put a String that starts with a number: Ej == > "43sgfdlgfd"

  • If I put a number. Ej == > "32214", "23532" ...

But I can not get it to work if it starts with a letter and contains some numerical character in the middle of Labor == > "Clean Cr124istales".

Any idea how I can do ???

Thank you very much everyone.

    
asked by Josemanuu 11.02.2018 в 16:48
source

1 answer

2

You can use the String method. matches that compares the string with a regular expression

if(test.matches(".*\d.*")) {
// error, contiene un dígito
}

In this case ".*\d.*" is a regular expression that specifies any text string that contains a digit.

    
answered by 11.02.2018 / 16:56
source