How To Validate Fields


 Validating the field Name.

The code above made in NetBeans is use to validate a field. In the example, we valindando field 'name', which is not allowed to enter numbers, only letters. Copy the code and give it a try!

try
    { 
        txtNome.getText().matches("[0-9]"); //
If you do not want to be typed letters put ("[a-zA-Z]")
                       
        JOptionPane.showMessageDialog(null, "Word invalid!\n T
he field only accepts characters: (Aa-Zz)."); 
         txtNome.setText("");            
    }
catch (Exception erro)
    { 
          JOptionPane.showMessageDialog(null,"Error");       
     }



Note: If you want you can implement the same code using IF and ELSE.


1 comment:

  1. Excelent blog!!
    Helped me a lot, thanks to the developers! you guys rock!!!

    ReplyDelete