Juju
Juju

Reputation: 59

Validation when Creating an Account JAVA

So there is this certain part of my program where I can create an account and the created account will be inserted into my database. And I'm trying to code something where *refer to the code:

public void actionPerformed(ActionEvent e) {
    String user = userField.getText().trim();
    String pass = passField.getText().trim();
    String conPass = confirmPass.getText().trim();  
try{    

    // TODO Auto-generated method stub
    if(e.getSource()==submit){

            if (user.equals(user)&&pass.length()==0){
                JOptionPane.showMessageDialog(null, "Fill in the empty field!");
            }//check if the pass field is blank
            else if(user.length()<5){
            JOptionPane.showMessageDialog(null,"Username must be at least 5 characters!");
            }
            else if(user.equals(user)&&pass.equals(conPass)&&pass.length()!=0){
                String sqlLogin = "insert into tblLogin (username,pssword) values ('"+user+"','"+pass+"')";;
                getQuery(sqlLogin); 
                JOptionPane.showMessageDialog(null, "Account Successfully Created!");
                create.dispose();
                GUI gui = new GUI();
            }//if(pass.equals(conPass))
            else if(user.length()==0&&pass.length()==0){
                JOptionPane.showMessageDialog(null, "Fill in the empty field!");
            }//check if both fields are blank
            else if (user.length()==0 &&pass.equals(pass)){
                JOptionPane.showMessageDialog(null, "Fill in the empty field!");
            }//check if user field is blank 
            else if(user.equals(user)&&pass!=conPass){
                JOptionPane.showMessageDialog(null, "Password do not match!");
            }//check if password and confirm pass matches
    }

I dont really know how to say the problem but look in the if and else if statements, if the user meet one the those conditions, the program should print the JOptionPane thing. Except for the second else if.

You might be wondering why I put these codes at my else if

else if(user.equals(user)&&pass.equals(conPass)&&pass.length()!=0){
                String sqlLogin = "insert into tblLogin (username,pssword) values ('"+user+"','"+pass+"')";;
                getQuery(sqlLogin); 
                JOptionPane.showMessageDialog(null, "Account Successfully Created!");
                create.dispose();

The reason for this is that, my program is having some logic error when I try to put it in if statement. Please help me with my code thanks :) Feel free to write a new code for me :DD

Upvotes: 0

Views: 369

Answers (1)

itwasntme
itwasntme

Reputation: 1462

i might try something like this:

public static boolean isSet(String s){
     if(s==null || "".equals(s)) return false;
     return true;
}
//.... your validation here
if(isSet(user) && isSet(pass) && isSet(conPass) && pass.equals(conPass)){
    //create account
}else{
  //smth wrong eg. if(!pass.equals(conPass) { //wrongpass }
}

Upvotes: 1

Related Questions