Hi I am very new for android and in my app I have Validations for Change password page.
That means the Password must contain minimum 8 characters at least 1 Alphabet
Simple and sort, just use below method for checking valid password,
public static boolean isValidPassword(String password) {
Matcher matcher = Pattern.compile("((?=.*[a-z])(?=.*\\d)(?=.*[A-Z])(?=.*[@#$%!]).{4,20})").matcher(password);
return matcher.matches();
}
and you can use this method as below
if (!isValidPassword(edtPassword.getText().toString())) {
errorDialog("Password must contain mix of upper and lower case letters as well as digits and one special charecter(4-20)");
}
easy piece of code for email field validation and password validation and check for minimum 8 characters in password field.
if (isValidEmail(et_regemail.getText().toString())&&etpass1.getText().toString().length()>7){
if (validatePassword(etpass1.getText().toString())) {
Toast.makeText(getApplicationContext(),"Go Ahead".....
}
else{
Toast.makeText(getApplicationContext(),"InvalidPassword".....
}
}else{
Toast.makeText(getApplicationContext(),"Invalid Email".....
}
public boolean validatePassword(final String password){
Pattern pattern;
Matcher matcher;
final String PASSWORD_PATTERN = "^(?=.*[0-9])(?=.*[A-Z])(?=.*
[@#$%^&+=!])(?=\\S+$).{4,}$";
pattern = Pattern.compile(PASSWORD_PATTERN);
matcher = pattern.matcher(password);
return matcher.matches();
}
public final static boolean isValidEmail(CharSequence target) {
if (target == null)
return false;
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
public static boolean passwordCharValidation(String passwordEd) {
String PASSWORD_PATTERN = "^(?=.*[A-Z])(?=.*[@_.]).*$";
Pattern pattern = Pattern.compile(PASSWORD_PATTERN);
Matcher matcher = pattern.matcher(passwordEd);
if (!passwordEd.matches(".*\\d.*") || !matcher.matches()) {
return true;
}
return false;
}
Passwords do not match
if (password.getText().toString() != reEnteredPassword.getText().toString()) {
//not match
}
For Empty Fields
if (password.getText().toString().isEmpty() || reEnteredPassword.getText().toString().isEmpty()) {
//Fieds empty error message
Less than 8 characters
if ((password.getText().toString().length() < 8) || (reEnteredPassword.getText().toString().length() < 8)) {
// less than 8 characters error message
}
Not having special characters
if (!password.getText().toString().matches( "^(?=.*[0-9])(?=.*[a-z])(?=.*[!@#$%^&*+=?-]).{8,15}$") || !reEnteredPassword.getText().toString().matches( "^(?=.*[0-9])(?=.*[a-z])(?=.*[!@#$%^&*+=?-]).{8,15}$")){
//not having special characters error message
}
Try this it works
public static boolean isPasswordValidMethod(final String password) {
Pattern pattern;
Matcher matcher;
final String PASSWORD_PATTERN = "^(?=.*[A-Za-z])(?=.*\\\\d)(?=.*[$@$!%*#?&])[A-Za-z\\\\d$@$!%*#?&]{8,}$""
pattern = Pattern.compile(PASSWORD_PATTERN);
matcher = pattern.matcher(password);
return matcher.matches();
}
public static boolean isValidPassword(String s) {
Pattern PASSWORD_PATTERN
= Pattern.compile(
"[a-zA-Z0-9\\!\\@\\#\\$]{8,24}");
return !TextUtils.isEmpty(s) && PASSWORD_PATTERN.matcher(s).matches();
}
to use it,
if(isValidPassword(password)){ //password valid}
You can set whatever symbol that you allowed here