how to validate a jtextfield from mvc that enter only numbers

0

An example of this is the validation that a textfield, its length is equal to 4 but, how would you do so that when entering you are only numbers and only letters?

public void keyTyped(KeyEvent e)
{
    if(e.getSource().equals(t.getTxtdesc()))
    {  
        String Cade=t.getTxtdesc().getText();
        if(Cade.length()==45)
        { 
            JOptionPane.showMessageDialog(null, "Ha superado el limite", "AVISO", JOptionPane.WARNING_MESSAGE);
            e.consume();

        }
    }
    
asked by Marwin José Yepez Vargas 08.05.2018 в 06:28
source

1 answer

0

Use Document and Filters.

See this answer: link

You can always modify the name of the filter and the methods (especially test) to create your own filter. In this case I would advise the use of regular expressions for the detection of a string that only contains a combination of numbers and letters.

Greetings.

David

    
answered by 08.05.2018 в 18:29