Validation
-
I want that in textbox if 0 is insert than it shows an alert message. The textbox insert numbers only. How will I validate it? please help thanx in advance.
-
I want that in textbox if 0 is insert than it shows an alert message. The textbox insert numbers only. How will I validate it? please help thanx in advance.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Validate extends JFrame
{
TextField t;
public Validate()
{
t= new TextField(10);
add(t);
t.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if ((key == KeyEvent.VK_0))
{
JOptionPane.showMessageDialog(null,"0 Was Clicked", "Note",JOptionPane.INFORMATION_MESSAGE);
}
else
if((key == KeyEvent.VK_1)||(key == KeyEvent.VK_2)||(key == KeyEvent.VK_3)||(key == KeyEvent.VK_4)||(key == KeyEvent.VK_5)||(key == KeyEvent.VK_6||(key == KeyEvent.VK_7)||(key == KeyEvent.VK_8)||(key == KeyEvent.VK_9)))
{
return;
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Input", "Note",JOptionPane.WARNING_MESSAGE);
String s = t.getText();
t.setText(s.replaceAll(e.getKeyChar()+"",""));} } } ); setTitle("Validate"); setLocationRelativeTo(null); pack(); setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE); setVisible(true); } public static void main(String\[\] args) { new Validate(); }
}
-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Validate extends JFrame
{
TextField t;
public Validate()
{
t= new TextField(10);
add(t);
t.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if ((key == KeyEvent.VK_0))
{
JOptionPane.showMessageDialog(null,"0 Was Clicked", "Note",JOptionPane.INFORMATION_MESSAGE);
}
else
if((key == KeyEvent.VK_1)||(key == KeyEvent.VK_2)||(key == KeyEvent.VK_3)||(key == KeyEvent.VK_4)||(key == KeyEvent.VK_5)||(key == KeyEvent.VK_6||(key == KeyEvent.VK_7)||(key == KeyEvent.VK_8)||(key == KeyEvent.VK_9)))
{
return;
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Input", "Note",JOptionPane.WARNING_MESSAGE);
String s = t.getText();
t.setText(s.replaceAll(e.getKeyChar()+"",""));} } } ); setTitle("Validate"); setLocationRelativeTo(null); pack(); setDefaultCloseOperation(JFrame.EXIT\_ON\_CLOSE); setVisible(true); } public static void main(String\[\] args) { new Validate(); }
}
Would it not be simpler to use a JFormattedTextField to ensure numeric input and an InputVerifier to check the value is not 0?
-
Would it not be simpler to use a JFormattedTextField to ensure numeric input and an InputVerifier to check the value is not 0?
True there are many ways to do it. Snippet 1: Integers
public Validate()
{
Text.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e)
{
char c = e.getKeyChar();
if (!(Character.isDigit(c) ||(c == KeyEvent.VK_BACK_SPACE) ||(c == KeyEvent.VK_DELETE)))
{
getToolkit().beep();
e.consume();
}
if( c == KeyEvent.VK_0)
{
Label.setText("0 entered");
Label.setForeground(Color.red);
}
else
{
Label.setText("ok");
Label.setForeground(Color.black);
}
}
});Snippet 2: Double
public class Validate()
{
DoubleValidate dv = new DoubleValidate();
public Validate()
{
Text.setDocument(dv);
add(Text);
}static class DoubleValidate extends PlainDocument { public void insertString(int offset,String string, AttributeSet attributes)throws BadLocationException { if (string == null) { return; } else { String newValue; int length = getLength(); if (length == 0) { newValue = string; } else { String currentContent = getText(0, length); StringBuffer currentBuffer = new StringBuffer(currentContent); currentBuffer.insert(offset, string); newValue = currentBuffer.toString(); } try { Double.parseDouble(newValue); super.insertString(offset, string,attributes); } catch (NumberFormatException exception) { Toolkit.getDefaultToolkit().beep(); } } } }
}
-
True there are many ways to do it. Snippet 1: Integers
public Validate()
{
Text.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent e)
{
char c = e.getKeyChar();
if (!(Character.isDigit(c) ||(c == KeyEvent.VK_BACK_SPACE) ||(c == KeyEvent.VK_DELETE)))
{
getToolkit().beep();
e.consume();
}
if( c == KeyEvent.VK_0)
{
Label.setText("0 entered");
Label.setForeground(Color.red);
}
else
{
Label.setText("ok");
Label.setForeground(Color.black);
}
}
});Snippet 2: Double
public class Validate()
{
DoubleValidate dv = new DoubleValidate();
public Validate()
{
Text.setDocument(dv);
add(Text);
}static class DoubleValidate extends PlainDocument { public void insertString(int offset,String string, AttributeSet attributes)throws BadLocationException { if (string == null) { return; } else { String newValue; int length = getLength(); if (length == 0) { newValue = string; } else { String currentContent = getText(0, length); StringBuffer currentBuffer = new StringBuffer(currentContent); currentBuffer.insert(offset, string); newValue = currentBuffer.toString(); } try { Double.parseDouble(newValue); super.insertString(offset, string,attributes); } catch (NumberFormatException exception) { Toolkit.getDefaultToolkit().beep(); } } } }
}
Yes, there are many ways to make something more complicated than it needs to be.