Reputation: 4242
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 1 Special Character, for this I tried the code below, but it's not working.
Please help me.
if(!isPasswordValidMethod(newPassword.getText().toString())){
System.out.println("Not Valid");
}else{
System.out.println("Valid");
}
// Validate password
private boolean isPasswordValidMethod(String password) {
String yourString = newPassword.getText().toString();
System.out.println("yourString is =" + yourString);
boolean isValid = false;
// ^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$
// ^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$
String expression = "^(?=.*[A-Za-z])(?=.*\\\\d)(?=.*[$@$!%*#?&])[A-Za-z\\\\d$@$!%*#?&]{8,}$";
CharSequence inputStr = password;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
System.out.println("if");
isValid = true;
}else{
System.out.println("else");
}
return isValid;
}
Upvotes: 19
Views: 60425
Reputation: 375
I got the same warning 4 times after pasting the code from @yuzuriha's answer into Android Studio. The warning is:
Redundant character escape '\!' in RegExp
Next I cut the unnecessary characters according to warning and check if the string has length 8 or more.
Now my updated code is:
private boolean isValidPassword(String s) {
Pattern PASSWORD_PATTERN = Pattern.compile("[a-zA-Z0-9!@#$]{8,24}");
return s.length() >= 8 && PASSWORD_PATTERN.matcher(s).matches();
}
Upvotes: 0
Reputation: 1
public static boolean isPasswordValidMethodTrueOrFalse(final String password) {
Pattern pattern;
Matcher matcher;
final String THE_REAL_PASSWORD_PATTERN = "^(?=.*[A-Za-z])(?=.*\\\\d)(?=.*[$@$!%*#?&])[A-Za-z\\\\d$@$!%*#?&]{8,}$""
pattern = Pattern.compile(THE_REAL_PASSWORD_PATTERN);
matcher = pattern.matcher(password);
return matcher.matches();
}
Upvotes: 0
Reputation: 51
You can solve this without using Regex. Here is the code part :
private fun hasUpperCase(data: CharSequence): Boolean {
val password = data.toString()
return password.toLowerCase(Locale.ROOT) == password
}
private fun hasLowerCase(data: CharSequence): Boolean {
val password = data.toString()
return password.toUpperCase(Locale.ROOT) == password
}
Upvotes: 1
Reputation: 47
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
}
Upvotes: -3
Reputation: 3195
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");
}
Upvotes: 40
Reputation: 465
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
Upvotes: 7
Reputation: 4041
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)");
}
Upvotes: 0
Reputation: 1384
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();
}
Upvotes: 0
Reputation:
String validPassword = "12345";
_Password_String = Password.getText().toString();
Matcher matcher = Pattern.compile(validPassword).matcher(_Password_String);
if (matcher.matches()) {
Log.e("d11", _Password_String);
Toast.makeText(getActivity(), "Password Match", Toast.LENGTH_LONG).show();
getFragmentManager().popBackStack();
} else {
Password.setError("Password");
Toast.makeText(getActivity(), "Password not Match", Toast.LENGTH_LONG).show();
}
Upvotes: 2
Reputation: 1977
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();
}
Upvotes: 1
Reputation: 362
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;
}
Upvotes: 2