0
public class LockAppActivity extends Activity{

    EditText pass1, pass2;
    Button back, next;
    SharedPreferences prefs;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lockapp);

        pass1 = (EditText) findViewById(R.id.edit1);
        pass2 = (EditText) findViewById(R.id.edit2);

        back = (Button) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {    
            public void onClick(View v1) {
                Intent setIntent = new Intent(Intent.ACTION_MAIN);
                setIntent.addCategory(Intent.CATEGORY_HOME);
                setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(setIntent); 
            }
        });

        next = (Button) findViewById(R.id.next);
        next.setOnClickListener(new View.OnClickListener() {
            String password1 = "";
            String password2 = "";

            public void onClick(View v){

                password1 = pass1.getText().toString();
                password2 = pass2.getText().toString();             

                if (!password1.equals("")){

                    if (password1.length() >= 15){
                        Pattern pattern = Pattern.compile("[[0-9]&&[a-z]&&[A-Z]]");
                        Matcher matcher = pattern.matcher(password1);   

                        if(matcher.matches()){                      

                            if (password1.equals(password2)){
                                //SavePreferences("Password", password1);
                                Intent intent = new Intent(LockAppActivity.this, PhoneNumActivity.class);
                                startActivity(intent);
                            }
                            else{
                                pass1.setText("");
                                pass2.setText("");
                                Toast.makeText(LockAppActivity.this,"Not equal",Toast.LENGTH_LONG).show();
                            }
                        }
                        else{
                            pass1.setText("");
                            pass2.setText("");
                            Toast.makeText(LockAppActivity.this,"Not matched",Toast.LENGTH_LONG).show();
                        }
                    }
                    else{
                        pass1.setText("");
                        pass2.setText("");
                        Toast.makeText(LockAppActivity.this,"Length",Toast.LENGTH_LONG).show();
                    }                       
                }
                else{
                    pass1.setText("");
                    pass2.setText("");
                    Toast.makeText(LockAppActivity.this,"Nothing",Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private void SavePreferences(String key, String value){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }
}

I faced problem when I entered a valid password but it pops out a toast stated "Not matched" and how I am going to save this password in the apps and when a newer password entered it will update and overwrite the old password

stema
  • 90,351
  • 20
  • 107
  • 135
Android_Rookie
  • 509
  • 2
  • 10
  • 25

2 Answers2

2

In the Java regex flavor, && is a set intersection operator. It's used only inside character classes. Your regex:

[[0-9]&&[a-z]&&[A-Z]]

...tries to match exactly one character, which must be a member of all three of the sets [0-9], [A-Z], and [a-z]. Obviously, there is no such character. The most common technique for validating passwords with a regex is to use a separate lookahead to match each of the required character types, then do a "for real" match to validate the length and overall composition. In your case it might look like this:

^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])[0-9A-Za-z]{15,}$

You're using the matches() method to apply the regex, so you don't really have to use the start and end anchors (^ and $), but it doesn't hurt, and it communicates your intent more clearly to anyone who has to read your code later on.

Note that I'm only correcting your regex to meets your stated criteria, not commenting on the criteria themselves. And if there are any errors in the Android-specific parts of the code, I wouldn't know.

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
0

You can validate the Password using Regualar Expression as below

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PasswordValidator{

  private Pattern pattern;
  private Matcher matcher;

  private static final String PASSWORD_PATTERN = 
          "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";

  public PasswordValidator(){
      pattern = Pattern.compile(PASSWORD_PATTERN);
  }

  /**
   * Validate password with regular expression
   * @param password password for validation
   * @return true valid password, false invalid password
   */
  public boolean validate(final String password){

      matcher = pattern.matcher(password);
      return matcher.matches();

  }
}

Refer Here for more: http://www.mkyong.com/regular-expressions/how-to-validate-password-with-regular-expression/

stema
  • 90,351
  • 20
  • 107
  • 135
Ponmalar
  • 6,871
  • 10
  • 50
  • 80
  • thanks for your answer, like for this one (?=.*\\d) means must contains one digit from 0-9 (refer to the link), is it same as must contains AT LEAST one? and for the symbol part, i think it is not possible to list out all the symbols from the keyboard right? is there any alternative to ensure the password contains at least one symbol? Thanks a lot – Android_Rookie May 15 '12 at 08:24
  • Refer: http://stackoverflow.com/questions/5576160/checking-a-password-for-upper-lower-numbers-and-symbols – Ponmalar May 17 '12 at 04:10