Hello Friends,
I want to set the length of the textfield in java...
Please check my below code....it works finely if I press the keys one by one slowly...
But suppose if I press any key for a longer time the actual length exceeds and so the validation is not done correctly...
Can anyone help me to set the length??
public class TextValidation extends KeyAdapter
{
public void keyReleased(KeyEvent key)
{
Object txt=key.getSource();
if(txt==txtPatientId)
{
char ch=key.getKeyChar();
String specialchar="!@#$%^&*()~?>'<:{}|+_/\".,;'][=-` \\";
if (txtPatientId.getText().trim().length() <= 10)
{
if (specialchar.indexOf(ch)>-1)
{
JOptionPane.showMessageDialog(null, "SPECIAL CHARACTER IS NOT ALLOWED","Title",JOptionPane.WARNING_MESSAGE);
txtPatientId.setText(txtPatientId.getText().substring(0, txtPatientId.getText().length()-1));
}
}
else
{
JOptionPane.showMessageDialog(null, "MAXIMUM 10 CHARACTERS","Title",JOptionPane.WARNING_MESSAGE);
txtPatientId.setText(txtPatientId.getText().substring(0, txtPatientId.getText().length()-1));
}
}
}
}