0

Here is the example that I am validating the PasswordField.

This code will validate to see if the password matches and if the password matches with the database, it will return the integer of 3.

/*
 * loginValidator method
 * The objective of this method is to return the integer value 
 * If the return value is 0 that means the password field is wrong from the database
 * If the return value is 1 that means the email field are not met
 * If the return value is 2 that means the password field are not met
 * If the return value is 3 that means the password and email are met and is correct from the database
 */
public static int loginValidator(String email, String password) {

    // Checks the email
    Pattern emailField = Pattern.compile(".+@.+\\.[a-z]+");
    Matcher m = emailField.matcher(email);
    boolean legitEmail = m.matches();

    // Checks the password
    /*
    Pattern passwordField = Pattern.compile(".*[A-Z].*[0-9]");
    m = passwordField.matcher(password);
    boolean legitPassword = m.matches();
    */
    boolean legitPassword = true;

    // This code will validate the credentials user have keyed in
    // If the email code is correct
    if (legitEmail) { 
        // If the password code is correct
        if (legitPassword) {

            // If the password belongs to the email
            if (MemberDA.retrievePasswordBooleanByEmail(email, password) == true)
            {
                return 3;           
            }
            else 
            {
                return 0;
            }

        }
        // If the password code is wrong
        else {
            return 2;
        }

        // If the email code is wrong
    }else {
        return 1;
    }

}

The method will be called and retrieve the value...

int tempNumber = validator.loginValidator(txtLoginEmail.getText(),txtLoginPassword.getText());

            if(tempNumber == 1) {
                pnLogin.setBounds(511, 200, 270, 320);
                System.out.println("Please enter an email.");
                lblError.setText("Please enter an email.");
            }
            else if(tempNumber == 2) {
                pnLogin.setBounds(511, 200, 270, 320);
                System.out.println("The password should have at least a capital letter and a numerical digit.");
                lblError.setText("<html>The password must have at least a <br />capital letter and a numerical digit.</html>");
            }
            else if(tempNumber == 0){   
                pnLogin.setBounds(511, 200, 270, 310);
                System.out.println("Wrong email or password.");
                lblError.setText("Wrong email or password.");
            }
            else if (tempNumber == 3) {
                AdminAccountListAllPanel contentPane = new AdminAccountListAllPanel(myFrame);
                myFrame.setContentPane(contentPane);
                myFrame.setVisible(true);
            }

Are there any suggestion you want to make to improve the code?

Thanks

Minelava
  • 221
  • 1
  • 7
  • 21
  • why to use a return value of `int` when Java provides `boolean`?, I think it would be proper to use `boolean` – Abubakkar Jan 22 '15 at 05:09
  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Jan 22 '15 at 05:10
  • 1
    Don't store passwords or other sensitive data within `String`, consider using a `char` array instead – MadProgrammer Jan 22 '15 at 05:11
  • 4
    You may want to ask this question in : https://codereview.stackexchange.com/ Would be more suitable there. – Pushkar Jan 22 '15 at 05:11
  • @AbubakkarRangara I believe the OP is returning `3` because they are validation the email as well – MadProgrammer Jan 22 '15 at 05:11
  • @MadProgrammer out of curiosity, can you please provide more information about what you have mentioned in your comment, about storing password in char? – Abubakkar Jan 22 '15 at 05:12
  • For [example](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords) – MadProgrammer Jan 22 '15 at 05:15

0 Answers0