posted 16 years ago
I have to create a program that enter some information including a zip code, but the user is not allowed to type ( 00000 ) . If the user does input those five digits a invalid input should be displayed.
This is what i have for that part:
private JTextField getzipCodeEntry() {
zipCodeEntry = new JTextField();
zipCodeEntry.setPreferredSize(new Dimension(200, 20));
zipCodeEntry.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
zipCodeAsEntered = zipCodeEntry.getText().trim();
// This deletes leading and trailing blanks in entry field, too
zipCodeEntry.setText(zipCodeAsEntered);
System.out.println(zipCodeAsEntered);
if (!zipCodeAsEntered.matches("\\d{5}"))
System.out.println("ZIP must be five digits");
if (zipCodeAsEntered.length() == 0)
System.out.println("Zip Code is required");
}
}); // end private JLabel getzipCodeLabel()
return zipCodeEntry;