0
How do l put a validation rules in a jTextField used to enter contact number?
the rule validates that users should not input characters that exceeds 10.and if the user enters anything other than a digit there should be a beep sound to alert the user.the in a jTextField for name,the user should only input a letters not numbers.
1 Réponse
0
To get the limit of 10 characters and the sound you can use this:
YourJTextFieldName.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if (YourJTextFieldName.getText().length() == 10) {
Toolkit.getDefaultToolkit().beep();
e.consume();
}
}});
To make it able to only use characters you can use this:
if (!(Pattern.matches("[A-Za-z ]*", YourJTextFieldName.getText()))) {
JOptionPane.showMessageDialog(null, "Please use characters only", "Error", JOptionPane.ERROR_MESSAGE);
}
Dont forget to import.
Try it out yourself if you really need help i will help you with the code