sean
sean

Reputation: 9258

Help with password validation

I created a login application. But when I input a wrong password, the app won't give me any notification that the password was incorrect and it's because I didn't even put any codes for it. I don't know what to put in order to achieve it. I'm a newbie here. Please help.

This is my code:

public void onClick(View v)
{ 
    EditText passwordEditText = (EditText) findViewById(R.id.currentPass);
    SharedPreferences prefs = this.getApplicationContext().getSharedPreferences("prefs_file",MODE_PRIVATE);
    String password = prefs.getString("password","");
    if("".equals(password))
    {
        Editor edit = prefs.edit();
        edit.putString("password",passwordEditText.getText().toString());
        edit.commit();
        StartMain();
    }
    else
    {
     if(passwordEditText.getText().toString().equals(password))
        {
             StartMain();
        }
    }

}

Upvotes: 0

Views: 496

Answers (4)

George
George

Reputation: 3797

I suggest using a toast to notify the user. Like this:

 else
    {
     if(passwordEditText.getText().toString().equals(password))
        {
             StartMain();
        }
    } else {
    Toast.makeText(v.getContext(), "Wrong password!", Toast.LENGTH_LONG).show();
}

Upvotes: 0

tinny_bug
tinny_bug

Reputation: 41

Hello schadi: i think you problem is this:EditText catch the click event as first as you click it that you should want to input some password.so,your code should always running at '"".equals(password)' statement. you may can use TextWatcher or something else to do your work.

Chinese guy,English is poor,i hope you can understand what i am saying :)

Upvotes: 0

Franci Penov
Franci Penov

Reputation: 75982

The simplest think would be to show a Toast notification. You could do that in the else branch of the second (nested) if in your code above.

Upvotes: 1

Tom Hamming
Tom Hamming

Reputation: 10961

You probably want an else condition on your inner if statement:

if(passwordEditText.getText().toString().equals(password)) //checking if they put the right password?
    {
         StartMain(); //I assume this is starting the application
    }
else
{
    //Tell them the password was wrong.
}

Upvotes: 2

Related Questions