
hi..........
thanks for ur reply for validation code.
but i want a very simple code in java swings where user is allowed to enter only numerical values in textbox , if he enters string values then it should give a message box that only numerical values should be entered. How can this be done????
plz help.......

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Validation extends JFrame {
JTextField text;
Container con;
JLabel label;
public Validation() {
con = getContentPane();
setBounds(0, 0, 500, 300);
text = new JTextField(25);
setLayout(new FlowLayout());
con.add(new JLabel("Enter the number"));
con.add(text);
con.add(label = new JLabel());
label.setForeground(Color.red);
setDefaultCloseOperation(EXIT_ON_CLOSE);
text.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent EVT) {
String value = text.getText();
int l = value.length();
if (EVT.getKeyChar() >= '0' && EVT.getKeyChar() <= '9') {
text.setEditable(true);
label.setText("");
} else {
text.setEditable(false);
label.setText("* Enter only numeric digits(0-9)");
}
}
});
show();
}
public static void main(String[] args) {
new Validation();
}
}
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.