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
In the Java regex flavor,
&&is a set intersection operator. It’s used only inside character classes. Your regex:…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: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.