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]")
{
txtNome.getText().matches("[0-9]"); //If you do not want to be typed letters put ("[a-zA-Z]")
JOptionPane.showMessageDialog(null, "Word invalid!\n The field only accepts characters: (Aa-Zz).");
txtNome.setText("");
}
catch (Exception erro)
{
JOptionPane.showMessageDialog(null,"Error");
}
}
catch (Exception erro)
{
JOptionPane.showMessageDialog(null,"Error");
}
Note: If you want
you can implement the same code using IF and ELSE.
Excelent blog!!
ReplyDeleteHelped me a lot, thanks to the developers! you guys rock!!!