Need help on figuring the checkPhone, checkEmail, and checkPassword!
OK, dudes. I figured out most of my code here and yes, I've tried it. However, I'm confused (as the title above mentions) on what I should do to get them working. Here are a couple of my theories that I have and my code is below:
checkEmail: I know it must use the "@" somewhere, so perhaps something that uses startsWith.
checkPhone: This method should test that the user has entered the first 3 digits of the phone as 972 or 214. If the first 3 digits of phone number are 972 or 214, then the phone label foreground should be set to black. If the first 3 digits of the phone are NOT 972 and NOT 214, then the phone label foreground should be set to red, so I'm assuming using substring, but I dunno.
checkPassword: Have 2 boxes for primary and verification, so...to be honest, I have no idea how I should check the text in the password textfield(primary) vs. the text in the retype password textfield(verification)...
Any info is truly appreciated.
Code:
import java.awt.*; //import older gui library for content pane
import javax.swing.*; //import newer gui library for labels, textfields, and button
import java.awt.event.*; //import gui event action library
public class RegistrationForm extends JComponent implements ActionListener {
JFrame frame = new JFrame("Registration Form");
Container content = frame.getContentPane();
// declare labels used on GUI screen
private JLabel labelName, labelAddress, labelCity, labelState, labelZip, labelPhone, labelEmail, labelPassword,labelrePassword;
private JLabel labelError, labelRegistration; // used for good and bad messages
// declare text fields used on GUI screen
private JTextField textName, textAddress, textCity, textState, textZip, textPhone, textEmail, textPassword,textrePassword;
// declare button used on GUI screen
private JButton submitButton, clearButton;
public static void main(String args[]) {
RegistrationForm application = new RegistrationForm();
}
public RegistrationForm() {
try {
content.add(this);
// create the name label
labelName = new JLabel(); //instantiate new JLabel
labelName.setText("Name"); //set label text to name
labelName.setLocation(38, 10); //set location of JLabel
labelName.setSize(200, 25); //set size of JLabel
labelName.setForeground(Color.BLACK);//set initial background color
content.add(labelName); //add JLabel to content pane
// create the name text box
textName = new JTextField(); //instantiate new JTextField
textName.setText(""); //clear JTextField
textName.setToolTipText("Please type in full name - last name first");
textName.setLocation(75, 10); //set location of JTextFfield
textName.setSize(200, 25); //set size of JTextField
content.add(textName); //add jextfield to content pane
// create the address label
labelAddress = new JLabel();
labelAddress.setText("Address");
labelAddress.setLocation(23, 50);
labelAddress.setSize(80, 25);
labelAddress.setForeground(Color.BLACK);
content.add(labelAddress);
// create the address text box
textAddress = new JTextField();
textAddress.setText("");
textAddress.setToolTipText("Please type in full address");
textAddress.setLocation(75, 50);
textAddress.setSize(300, 25);
content.add(textAddress);
// create the city label
labelCity = new JLabel();
labelCity.setText("City");
labelCity.setLocation(48, 90);
labelCity.setSize(300, 25);
labelCity.setForeground(Color.BLACK);
content.add(labelCity);
// create the city text box
textCity = new JTextField();
textCity.setText("");
textCity.setToolTipText("Please type in full city");
textCity.setLocation(75, 90);
textCity.setSize(130, 25);
content.add(textCity);
// create the state label
labelState = new JLabel();
labelState.setText("State");
labelState.setLocation(40, 130);
labelState.setSize(300, 25);
labelState.setForeground(Color.BLACK);
content.add(labelState);
// create the state text box
textState = new JTextField();
textState.setText("");
textState.setToolTipText("Choose State");
textState.setLocation(75, 130);
textState.setSize(130, 25);
content.add(textState);
// create the zip label
labelZip = new JLabel();
labelZip.setText("Zip Code");
labelZip.setLocation(23, 170);
labelZip.setSize(300, 25);
labelZip.setForeground(Color.BLACK);
content.add(labelZip);
// create the zip text box
textZip = new JTextField();
textZip.setText("");
textZip.setToolTipText("Zip code must be between 10000 and 99999 inclusive");
textZip.setLocation(75, 170);
textZip.setSize(130, 25);
content.add(textZip);
// create the phone label
labelPhone = new JLabel();
labelPhone.setText("Phone Number");
labelPhone.setLocation(23, 210);
labelPhone.setSize(300, 25);
labelPhone.setForeground(Color.BLACK);
content.add(labelPhone);
// create the phone text box
textPhone = new JTextField();
textPhone.setText("");
textPhone.setToolTipText("Phone number must be 10 characters long");
textPhone.setLocation(110, 210);
textPhone.setSize(130, 25);
content.add(textPhone);
// create the email label
labelEmail = new JLabel();
labelEmail.setText("E-Mail");
labelEmail.setLocation(23, 250);
labelEmail.setSize(300, 25);
labelEmail.setForeground(Color.BLACK);
content.add(labelEmail);
// create the email text box
textEmail= new JTextField();
textEmail.setText("");
textEmail.setToolTipText("Please enter a valid email address");
textEmail.setLocation(75, 250);
textEmail.setSize(180, 25);
content.add(textEmail);
// create the password label
labelPassword = new JLabel();
labelPassword.setText("Password");
labelPassword.setLocation(23, 290);
labelPassword.setSize(300, 25);
labelPassword.setForeground(Color.BLACK);
content.add(labelPassword);
// create the password text box
textPassword= new JTextField();
textPassword.setText("");
textPassword.setToolTipText("Please enter a valid email address");
textPassword.setLocation(90, 290);
textPassword.setSize(230, 25);
content.add(textPassword);
// create the confirmation password label
labelrePassword= new JLabel();
labelrePassword.setText("Password re-enter");
labelrePassword.setLocation(23, 330);
labelrePassword.setSize(300, 25);
labelrePassword.setForeground(Color.BLACK);
content.add(labelrePassword);
// create the confirmation password text box
textrePassword= new JTextField();
textrePassword.setText("");
textrePassword.setToolTipText("Password must match in order to continue");
textrePassword.setLocation(140, 330);
textrePassword.setSize(230, 25);
content.add(textrePassword);
// create the submit button
submitButton = new JButton();
submitButton.setText("Submit");
submitButton.setToolTipText("Click \"submit \" when the form is completely filled out");
submitButton.setLocation(125, 450);
submitButton.setSize(100, 30);
content.add(submitButton);
submitButton.addActionListener(this);
clearButton = new JButton();
clearButton.setText("Clear");
clearButton.setToolTipText("Click \"clear \" when you want to clear the form");
clearButton.setLocation(250, 450);
clearButton.setSize(100, 30);
content.add(clearButton);
clearButton.addActionListener(this);
// create the registration label
labelRegistration = new JLabel();
labelRegistration.setText("Thank you for your registration.");
labelRegistration.setLocation(145, 500);
labelRegistration.setSize(190, 25);
labelRegistration.setForeground(Color.BLACK);
labelRegistration.setVisible(false);
content.add(labelRegistration);
// create the error label
labelError = new JLabel();
labelError.setText("Please correct items in red");
labelError.setLocation(150, 500);
labelError.setSize(190, 25);
labelError.setForeground(Color.RED);
labelError.setVisible(false);
content.add(labelError);
// set properties of window
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(475, 600);
frame.setLayout(null);
frame.setVisible(true);
} catch (Exception e) {
}
}
public void actionPerformed(ActionEvent event) {
try {
if (event.getActionCommand().equals("Submit")) {
//Statements get executed here when SUBMIT button is pressed.
//Using the "&" instead of "&&" ensures that all check methods get called.
//If you used the "&&", you would only see one error at a time.
//The form cannot be submitted until all the errors are fixed (every method returns true).
if (checkName() & checkAddress() & checkCity() & checkState() & checkZip()& checkPhone()& checkrePassword()& checkEmail()& checkPassword()) {
labelRegistration.setVisible(true); //all information is entered and correct
labelError.setVisible(false); //hide this message if visible
} else {
labelError.setVisible(true); //all information is not valid on form
labelRegistration.setVisible(false); //hide this message when bad information entered
}
} else {
textName.setText("");
textAddress.setText("");
textCity.setText("");
textState.setText("");
textZip.setText("");
textPhone.setText("");
textEmail.setText("");
textPassword.setText("");
textrePassword.setText("");
labelError.setVisible(false);
labelRegistration.setVisible(false);
labelName.setForeground(Color.BLACK);
labelAddress.setForeground(Color.BLACK);
labelCity.setForeground(Color.BLACK);
labelState.setForeground(Color.BLACK);
labelZip.setForeground(Color.BLACK);
labelPhone.setForeground(Color.BLACK);
labelEmail.setForeground(Color.BLACK);
labelPassword.setForeground(Color.BLACK);
labelrePassword.setForeground(Color.BLACK);
}
} catch (Exception e) {
}
}
/* The checkName method looks at the contents of the name text field.
The method requires that the length is greater than zero to pass (return true).
If the length is greater than zero, the method returns true and sets the label
color to black. If the length is zero, the method returns false and changes the
label color to red. */
private boolean checkName() {
if (textName.getText().length() == 0) {
labelName.setForeground(Color.RED); //name is not correct
return false;
} else {
labelName.setForeground(Color.BLACK); //name is correct
return true;
}
}
/* The checkAddress method looks at the contents of the address text field.
The method requires that the length is at least 5 to pass (return true).
If the length is at least 5, the method returns true and sets the label
color to black. If the length is less than 5, the method returns false and
changes the label color to red. */
private boolean checkAddress() {
if (textAddress.getText().length() < 5) {
labelAddress.setForeground(Color.RED); //address is not correct
return false;
} else {
labelAddress.setForeground(Color.BLACK); //address is correct
return true;
}
}
/* The checkCity method looks at the contents of the city text field.
The method requires that the length is greater than zero to pass (return true).
If the length is greater than zero, the method returns true and sets the label
color to black. If the length is zero, the method returns false and changes the
label color to red. */
private boolean checkCity() {
if (textCity.getText().length() == 0) {
labelCity.setForeground(Color.RED); //city is not correct
return false;
} else {
labelCity.setForeground(Color.BLACK); //city is correct
return true;
}
}
/* The checkState method looks at the contents of the state text field.
The method requires that the state be in the state array. The for loop goes through every state in the state array
checking to see if the state that the user entered was found (sets the isFound variable at true). If it finishes the loop
and the isFound variable was never set to true, then that user's state was not found in the state array. If the state was
found, the method returns true and sets the label color to black. If the state was not found, the method returns false
and changes the label color to red. */
private boolean checkState() {
String stateArray[] = {"al", "ak", "az", "ar", "ca", "co", "ct", "de", "dc", "fl", "ga", "hi", "id", "il", "in",
"ia", "ks", "ky", "la", "me", "md", "ma", "mi", "mn", "ms", "mo", "mt", "ne", "nv", "nh", "nj", "nm",
"nc", "nd", "oh", "ok", "or", "pa", "ri", "sc", "sd", "tn", "tx", "ut", "vt", "va", "wa", "wv", "wi", "wy"};
boolean isFound = false;
for (int i = 0; i < stateArray.length; i++) {
if (textState.getText().equalsIgnoreCase(stateArray[i])) {
isFound = true;
}
}
if (isFound == true) {
labelState.setForeground(Color.BLACK); //state is correct
return true;
} else {
labelState.setForeground(Color.RED); //state is not correct
return false;
}
}
/* The checkZip method looks at the contents of the zip code field.
The method requires that the zip code be an integer and be a length of 5 to pass. If it passes it would return true
and set the label color to black. If zip code isn't a number or is less/more than 5 characters long, the method
would return false and set the label color to red. */
private boolean checkZip() {
try {
Integer.parseInt(textZip.getText()); //zip is a number
if (textZip.getText().length() == 5) {
labelZip.setForeground(Color.BLACK); //zip is correct length
return true;
} else {
labelZip.setForeground(Color.RED); //zip is not correct length
return false;
}
} catch (Exception e) {
labelZip.setForeground(Color.RED); //zip is not a number
return false;
}
}
//////////////////////////////////////////////////////my code
private boolean checkPhone ()
{
if ((textPhone.getText().substring(0,4).equals("972")) && (textPhone.getText().substring(0,4).equals("214")))
{
labelPhone.setForeground(Color.BLACK); //phone is correct
return true;
}
else
{
labelPhone.setForeground(Color.RED); //phone is incorrect
return false;
}
}//end of method
private boolean checkEmail() {
try {
Integer.parseInt(textEmail.getText()); //email is a number
if (textEmail.getText().length() == 10) {
labelZip.setForeground(Color.BLACK);
return true;
} else {
labelEmail.setForeground(Color.RED);
return false;
}
} catch (Exception e) {
labelEmail.setForeground(Color.RED);
return false;
}
}
private boolean checkPassword() {
try {
Integer.parseInt(textPassword.getText());
if (textPassword.getText().length() == 10) {
labelZip.setForeground(Color.BLACK);
return true;
} else {
labelPassword.setForeground(Color.RED);
return false;
}
} catch (Exception e) {
labelPassword.setForeground(Color.RED);
return false;
}
}
private boolean checkrePassword() {
try {
Integer.parseInt(textrePassword.getText());
if (textrePassword.getText().length() == 10) {
labelZip.setForeground(Color.BLACK);
return true;
} else {
labelrePassword.setForeground(Color.RED);
return false;
}
} catch (Exception e) {
labelrePassword.setForeground(Color.RED);
return false;
}
}
}
Re: Need help on figuring the checkPhone, checkEmail, and checkPassword!
Please read the forum FAQ and add code tags around your code:
[code]
// your posted code goes here
[/code]
Else it is too hard to read.
Re: Need help on figuring the checkPhone, checkEmail, and checkPassword!
thanks, so what do I need to do above to make it work?