How To Validate Password Field In Android?
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, 1 Number and
Solution 1:
try following Code
//*****************************************************************
public static boolean isValidPassword(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();
}
And change your code to this
if(newPassword.getText().toString().length()<8 &&!isValidPassword(newPassword.getText().toString())){
System.out.println("Not Valid");
}else{
System.out.println("Valid");
}
Solution 2:
publicstatic boolean isValidPassword(String s) {
PatternPASSWORD_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
Solution 3:
publicstaticbooleanpasswordCharValidation(String passwordEd) {
StringPASSWORD_PATTERN="^(?=.*[A-Z])(?=.*[@_.]).*$";
Patternpattern= Pattern.compile(PASSWORD_PATTERN);
Matchermatcher= pattern.matcher(passwordEd);
if (!passwordEd.matches(".*\\d.*") || !matcher.matches()) {
returntrue;
}
returnfalse;
}
Solution 4:
Try this it works
publicstatic boolean isPasswordValidMethod(finalString password) {
Pattern pattern;
Matcher matcher;
finalStringPASSWORD_PATTERN="^(?=.*[A-Za-z])(?=.*\\\\d)(?=.*[$@$!%*#?&])[A-Za-z\\\\d$@$!%*#?&]{8,}$""
pattern = Pattern.compile(PASSWORD_PATTERN);
matcher = pattern.matcher(password);
return matcher.matches();
}
Solution 5:
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".....
}
publicboolean validatePassword(finalString password){
Pattern pattern;
Matcher matcher;
finalString PASSWORD_PATTERN = "^(?=.*[0-9])(?=.*[A-Z])(?=.*
[@#$%^&+=!])(?=\\S+$).{4,}$";
pattern = Pattern.compile(PASSWORD_PATTERN);
matcher = pattern.matcher(password);
return matcher.matches();
}
publicfinalstaticboolean isValidEmail(CharSequence target) {
if (target == null)
returnfalse;
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
Post a Comment for "How To Validate Password Field In Android?"