How to validate Password Field in android?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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 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;





Maybe this answer will help you: stackoverflow.com/questions/9962382/…
– Francisco Nin
Apr 12 '16 at 13:07




7 Answers
7



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");





if(newPassword.getText().toString().length()<8 ||!isValidPassword(newPassword.getText().toString())){ System.out.println("Not Valid");
– Krish
Apr 12 '16 at 13:28





that is validate condition
– Krish
Apr 12 '16 at 13:28





minimum 8 characters means it must be "&&" not ||
– mdDroid
Apr 12 '16 at 13:42





What would be conditions for this pattern like password must contain min 8 characters, must be alpha numeric,must contain at least one symbol. Like this? @mdDroid
– Sid
Nov 12 '16 at 9:31





thanks working very well
– Anand Kumar Jha
Feb 10 '17 at 9:17


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


public static boolean passwordCharValidation(String passwordEd)





Is you answer full-fill :It contains minimum 8 characters at least 1 Alphabet, 1 Number and 1 Special Character.Thanks
– user4856296
Apr 12 '16 at 13:17





yes Firstly check passwordEd length greater then 8 if(passwordEd.length() > 8);
– Nitesh Pareek
Apr 12 '16 at 13:23




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();



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();



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();



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)");






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered