How do l put a validation rules in a jTextField used to enter contact number? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

12th Oct 2018, 12:43 PM
Sandra Susan Sibanda
Sandra Susan Sibanda - avatar
1 Answer
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
13th Oct 2018, 11:53 AM
JavaBobbo
JavaBobbo - avatar