
I am new to java and i am making a registration form.i just want to know if there is any code in which a string cannot be inserted in a jtextfield where a phone number has to be inserted.the jtextfield should not respond to any character in jtextfield.plzz help

Here is an example that accept the numbers only through the jtextfield.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class TextValidation extends JFrame{
JTextField text;
JLabel label;
JPanel p;
TextValidation(){
text=new JTextField(15);
label=new JLabel("Enter number:");
p=new JPanel();
p.add(label);
p.add(text);
text.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
char ch = e.getKeyChar();
if(Character.isDigit(ch)){
}
else{
JOptionPane.showMessageDialog(null, "Only numbers are allowed!");
text.setText(" ");
}
}
});
add(p);
setVisible(true);
setSize(300,100);
}
public static void main(String[]args){
TextValidation v=new TextValidation();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.