简体   繁体   中英

validation text field unexpected outcome

currently im working on a code for java netbeans to create a functioning login form. my code runs perfectly, but the only issue is my validation is not working the way i want it to be. like for example, when i leave the username text field for my form. it shows me the prompt to enter the credentials. but once i press dismiss for that prompt. it submits the form with the empty text field. Here is my code below :

what am i doing wrong?

   try{
        String username;
        username = usernameTxt.getText();
        String password;
        password= new String (passwordTxt.getText());
     if(username.equals("")){
        JOptionPane.showMessageDialog(null,"Please Enter Your Username.");
    }
    if(password.equals("")){
        JOptionPane.showMessageDialog(null,"Please Enter Your Password.");
    }

    String confirmpassword = new String (confirmpassTxt.getText());

    if(password.equals(confirmpassword)){
       confirmpassLab.setText("");
    }
    else {
    confirmpassLab.setText("PASSWORD DOES NOT MATCH.");
    }
        PrintWriter pw = new PrintWriter (new BufferedWriter(new FileWriter("ResidentLoginCredentials.txt",true))); // TRUE is to get all the information from the form to send it into the database. this code is to send the information to the databse 
        pw.println(username); // passing that value to the file 
        pw.println(password); // passing the name value to the database 
        pw.close();
        JOptionPane.showMessageDialog(this,"Your Account Has Been Successfully Registered.","File Message", JOptionPane.INFORMATION_MESSAGE);
        usernameTxt.setText("");
        passwordTxt.setText("");
        this.setVisible(false); // this makes the current form disappear and the new form to open
    residentassignunit ru = new residentassignunit(); // this codes are to redirect user to another form
    ru.setVisible(true);

   }
   catch(IOException e)
   {
        JOptionPane.showMessageDialog(this,"File Not Found","File Error Message Box",JOptionPane.ERROR_MESSAGE);
   }
try{
        String username;
        username = usernameTxt.getText();
        String password;
        password= new String (passwordTxt.getText());

     if(username.equals("")){
        JOptionPane.showMessageDialog(null,"Please Enter Your Username.");
    }else if(password.equals("")){
        JOptionPane.showMessageDialog(null,"Please Enter Your Password.");
    }else if(!password.equals(confirmpassword)){
       confirmpassLab.setText("PASSWORD DOES NOT MATCH.");          
    }else{
       String confirmpassword = "";    

        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("ResidentLoginCredentials.txt",true))); // TRUE is to get all the information from the form to send it into the database.this code is to send the information to the databse 

        pw.println(username); // passing that value to the file 
        pw.println(password); // passing the name value to the database 
        pw.close();
        JOptionPane.showMessageDialog(this,"Your Account Has Been Successfully Registered.","File Message", JOptionPane.INFORMATION_MESSAGE);
        usernameTxt.setText("");
        passwordTxt.setText("");
        this.setVisible(false); // this makes the current form disappear and the new form to open
    residentassignunit ru = new residentassignunit(); // this codes are to redirect user to another form
    ru.setVisible(true);
  }    
   }
   catch(IOException e)
   {
        JOptionPane.showMessageDialog(this,"File Not Found","File Error Message Box",JOptionPane.ERROR_MESSAGE);
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM