Hi, I posted the registration form question last night, but I have figured most of it out on my own. I was wondering if someone could please help me with one method that i need to get to work as well as a way to show a label if there arent any errors.
The method that i need help with is a zip code check. The zip has to be greater than or equal to 10000 and less than or equal to 20000.
The label part that im having troubles with is if a user enters all the info in the form without any errors, "thank you" will appear in a label. If there are errors, the label would say something like "please fix the areas that are red."
Here is my code and everything works correctly except those 2 problems stated above.
package program2;
// This program creates a small online form
// Designed by Nate Hultman
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
publicclass Program2 extends Applet implements ActionListener
{
// These are global objects for textfields, labels, and buttons
private TextField nameBox = new TextField("",25); // room for 25 chars and empty
private TextField addressBox = new TextField("",25);
private TextField cityBox = new TextField("",25);
private TextField stateBox = new TextField("",2);
private TextField zipBox = new TextField("",10);
private TextField phoneBox = new TextField("",15);
private TextField emailBox = new TextField("",25);
private TextField productIDBox = new TextField("",15);
Label nameLabel = new Label ("Name:") ;
Label addressLabel = new Label ("Address:");
Label cityLabel = new Label ("City:");
Label stateLabel = new Label ("State:");
Label zipLabel = new Label ("Zip Code:");
Label phoneLabel = new Label ("Phone:");
Label emailLabel = new Label ("Email:");
Label productIDLabel = new Label ("Product ID:");
private Button submit = new Button ("Submit");
publicvoid init()
{
setBackground(Color.blue);
//layout for overall applet is GridLayout with 9 rows, 2 columns
setLayout(new GridLayout(9,1));
// first row
add(nameLabel);
add(nameBox);
// second row
add(addressLabel );
add(addressBox);
// third row
add(cityLabel);
add(cityBox);
// fourth row
add(stateLabel );
add(stateBox);
// fifth row
add(zipLabel );
add(zipBox);
// sixth row
add(phoneLabel );
add(phoneBox);
// seventh row
add(emailLabel);
add(emailBox);
// eighth row
add(productIDLabel);
add(productIDBox);
// ninth row
add(submit);
submit.addActionListener(this);
}// ends init method
publicvoid actionPerformed( ActionEvent e )
{ //Declaring all the methods
checkName ();
checkAddress ();
checkCity ();
checkState();
checkZip ();
checkPhone();
checkEmail();
checkLabel();
}
//All the error checking methods
publicvoid checkName ( )
{
String name = nameBox.getText();
if (name.length() == 0) //checks lenght is at least one character
nameLabel.setForeground(Color.red);
else
nameLabel.setForeground(Color.yellow);
}
publicvoid checkAddress(){
String address = addressBox.getText();
if (address.length() < 4) //checks lenght is at least one character
addressLabel.setForeground(Color.red);
else
addressLabel.setForeground(Color.yellow);
}
publicvoid checkCity(){
String city = cityBox.getText();
if (city.length() == 0) //checks lenght is at least one character
cityLabel.setForeground(Color.red);
else
cityLabel.setForeground(Color.yellow);
}
publicvoid checkState(){
String state = stateBox.getText();
if (state.equals("PA") || state.equals("NY")
|| state.equals("pa") || state.equals("ny"))
stateLabel.setForeground(Color.yellow);
else
stateLabel.setForeground(Color.red);
}
//PLEASE HELP ME WITH THIS PART
publicvoid checkZip(){
String zip = zipBox.getText();
if (zip.startsWith("0") || zip.equals(zip => '20001'))
zipLabel.setForeground(Color.red);
else
zipLabel.setForeground(Color.yellow);
}
publicvoid checkPhone(){
String phone = phoneBox.getText();
if (phone.startsWith("702") || phone.startsWith("412")) //checks lenght is at least one character
phoneLabel.setForeground(Color.yellow);
else
phoneLabel.setForeground(Color.red);
}
publicvoid checkEmail(){
String email = emailBox.getText( ); // gets the text from the email box
if (email.indexOf ( '@' ) == -1 ) // -1 means no occurence of @ sign in email
emailLabel.setForeground(Color.red);
else
emailLabel.setForeground(Color.yellow);
}
//PLEASE HELP ME WITH THIS
publicvoid checkLabel(){
}
} // end program
publicvoid checkZip(){
String zip = zipBox.getText();
int zipNum = 0 ;
// Make sure ZIP is a number
try {
zipNum = Integer.parseInt(zip);
if ( zipNum >= 10000 && zipNum <= 20000 ) {
// Zip is ok
zipLabel.setForeground(Color.yellow);
}
else {
// Zip is not in range
zipLabel.setForeground(Color.red);
}
}
catch (Exception e ) {
// Zip entered is not a number
// Do what you want to do in this error condition
zipLabel.setForeground(Color.red);
}
}