How to Validate Password Field in Android

How to validate Password Field in android?

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

setting Password pattern for validation of Password in android

You can use this.

private static final String PASSWORD_PATTERN = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";

Description of Pattern

  (?=.*\d)      #   must contains one digit from 0-9
(?=.*[a-z]) # must contains one lowercase characters
(?=.*[A-Z]) # must contains one uppercase characters
(?=.*[@#$%]) # must contains one special symbols in the list "@#$%"
. # match anything with previous condition checking
{6,20} # length at least 6 characters and maximum of 20

You can use like this

public boolean validate(final String password){

return PASSWORD_PATTERN.matches(password);
}

For detail please Check this

Regular Expression In Android for Password Field

Try this may helps

^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{4,}$

How it works?

^                 # start-of-string
(?=.*[0-9]) # a digit must occur at least once
(?=.*[a-z]) # a lower case letter must occur at least once
(?=.*[A-Z]) # an upper case letter must occur at least once
(?=.*[@#$%^&+=]) # a special character must occur at least once you can replace with your special characters
(?=\\S+$) # no whitespace allowed in the entire string
.{4,} # anything, at least six places though
$ # end-of-string

How to Implement?

public class MainActivity extends Activity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final EditText editText = (EditText) findViewById(R.id.edtText);
Button btnCheck = (Button) findViewById(R.id.btnCheck);

btnCheck.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
if (isValidPassword(editText.getText().toString().trim())) {
Toast.makeText(MainActivity.this, "Valid", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "InValid", Toast.LENGTH_SHORT).show();
}
}
});

}

public boolean isValidPassword(final String password) {

Pattern pattern;
Matcher matcher;

final String PASSWORD_PATTERN = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{4,}$";

pattern = Pattern.compile(PASSWORD_PATTERN);
matcher = pattern.matcher(password);

return matcher.matches();

}

}

how to check password and confirm password in field in android?

You need to match both fields euals or not after checking email pattern..Change your code of sub button like this....

sub.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
RadioGroup rgrp = (RadioGroup) findViewById(R.id.rg);
em = (EditText) findViewById(R.id.email);
RadioButton radioButton;
int selectedId = rgrp.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioButton = (RadioButton) findViewById(selectedId);
rval = radioButton.getText().toString();
// Toast.makeText(RegistrationForm.this, rval, Toast.LENGTH_SHORT).show();


if(validate){
new CreateNewProduct().execute();
// startActivity(new Intent(RegistrationForm.this, Home.class));
}
}
private boolean validate() {
boolean temp=true;
String checkemail = em.getText().toString();
String pass=pw.getText().toString();
String cpass=cpw.getText().toString();
if(!EMAIL_ADDRESS_PATTERN.matcher(checkemail).matches()){
Toast.makeText(Registratiomform.this,"Invalid Email Address",Toast.LENGTH_SHORT).show();
temp=false;
}
else if(!pass.equals(cpass)){
Toast.makeText(Registratiomform.this,"Password Not matching",Toast.LENGTH_SHORT).show();
temp=false;
}
return temp;
}
});

Password validation confirmation in android

You cannot compare strings using = or !=, use equals instead

else if(!conpass.getText().toString().equals(pass.getText().toString()) )

How to validate a password field for a specific password?

public class MainActivity extends AppCompatActivity {
Button buttn;
EditText passwrd;
String str, str1 = "coolhack";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
passwrd = (EditText) findViewById(R.id.password);
buttn = (Button) findViewById(R.id.button1);
buttn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
str = passwrd.getText().toString().trim();
validate();
}
});
}

private void validate() {
if(str.equals(str1)) {
Intent i = new Intent(MainActivity.this, Main2Activity.class);
startActivity(i);
} else {
Toast.makeText(MainActivity.this, "INVALID PASSWORD", Toast.LENGTH_SHORT).show();
}
}
}

Get the string from edittext when we click on the button.

Password Validation regex android

public class Validation {

public static void main(String[] args) {
String pass = "1AB%CDef555";
String username = "manna";
String email = "mannx@rtt.com";
System.out.println(validiate2(pass, username,email));
}
// if you don't care why it fails and only want to know if valid or not
public static boolean validiate (String pass, String username, String email){
String pattern = "^(?=.*[0-9])(?=.*[a-z])(?=.*[!@#$%^&*+=?-]).{8,15}$";
if(pass.matches(pattern)){
for(int i=0;(i+3)<username.length();i++){
if(pass.contains(username.substring(i,i+3)) || username.length()<3 || username.length()>15){
return false;
}
}
for(int i=0;(i+3)<email.length();i++){
if(pass.contains(email.substring(i,i+3)) || email.length()<3 || email.length()>15){
return false;
}
}
return true;
}
return false;
}
// if you want to know which requirement was not met
public static boolean validiate2 (String pass, String username, String email){
if (pass.length() < 8 || pass.length() >15 ){
System.out.println("pass too short or too long");
return false;
}
if (username.length() < 3 || username.length() >15 ){
System.out.println("username too short or too long");
return false;
}
if (!pass.matches(".*\\d.*")){
System.out.println("no digits found");
return false;
}

if (!pass.matches(".*[a-z].*")) {
System.out.println("no lowercase letters found");
return false;
}
if (!pass.matches(".*[!@#$%^&*+=?-].*")) {
System.out.println("no special chars found");
return false;
}
if (containsPartOf(pass,username)) {
System.out.println("pass contains substring of username");
return false;
}
if (containsPartOf(pass,email)) {
System.out.println("pass contains substring of email");
return false;
}
return true;
}

private static boolean containsPartOf(String pass, String username) {
int requiredMin = 3
for(int i=0;(i+requiredMin)<username.length();i++){
if(pass.contains(username.substring(i,i+requiredMin))){
return true;
}
}
return false;
}
}

password validation with Kotlin

You can do like this ...

internal fun isValidPassword(password: String): Boolean {
if (password.length < 8) return false
if (password.filter { it.isDigit() }.firstOrNull() == null) return false
if (password.filter { it.isLetter() }.filter { it.isUpperCase() }.firstOrNull() == null) return false
if (password.filter { it.isLetter() }.filter { it.isLowerCase() }.firstOrNull() == null) return false
if (password.filter { !it.isLetterOrDigit() }.firstOrNull() == null) return false

return true
}


Related Topics



Leave a reply



Submit