验证Java文本字段的最佳方法

验证Java文本字段的最佳方法,java,validation,Java,Validation,下面是我正在验证PasswordField的示例 此代码将进行验证,以查看密码是否匹配,如果密码与数据库匹配,则返回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

下面是我正在验证PasswordField的示例

此代码将进行验证,以查看密码是否匹配,如果密码与数据库匹配,则返回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;
    }

}
将调用该方法并检索值

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);
            }
int tempNumber=validator.loginValidator(txtLoginEmail.getText(),txtLoginPassword.getText());
if(tempNumber==1){
pnLogin.setBounds(511200270320);
System.out.println(“请输入电子邮件”);
lblError.setText(“请输入电子邮件”);
}
else if(tempNumber==2){
pnLogin.setBounds(511200270320);
System.out.println(“密码应该至少有一个大写字母和一个数字。”);
lblError.setText(“密码必须至少有一个
大写字母和一个数字。”); } 如果(tempNumber==0){ pnLogin.setBounds(511200270310); System.out.println(“错误的电子邮件或密码”); lblError.setText(“错误的电子邮件或密码”); } else if(tempNumber==3){ AdminAccountListalPanel contentPane=新的AdminAccountListalPanel(myFrame); setContentPane(contentPane); myFrame.setVisible(true); }
您是否有任何改进代码的建议


感谢

当Java提供了
布尔
时,为什么要使用返回值
int
?我认为使用
布尔
是合适的,避免使用
null
布局,像素完美的布局在现代ui设计中是一种幻觉。影响零部件单个尺寸的因素太多,您无法控制。Swing旨在与核心的布局管理器配合使用,丢弃这些布局管理器将导致无休止的问题,您将花费越来越多的时间试图纠正这些问题,而不是在
字符串中存储密码或其他敏感数据,考虑使用<代码> char 数组,你可能想问这个问题:在那里会更合适。“AbbAkkStutaRa我相信OP正在返回<代码> 3 < /代码>,因为它们也验证了电子邮件。