Regular Expression In Android for Password Field

Solution 1:

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

    }

}

Solution 2:

And for the Kotlin lovers :

fun isValidPassword(password: String?) : Boolean {
   password?.let {
       val passwordPattern = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{4,}$"
       val passwordMatcher = Regex(passwordPattern)

       return passwordMatcher.find(password) != null
   } ?: return false
}

Solution 3:

Try this.

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


(/^
(?=.*\d)                //should contain at least one digit
(?=.*[@$%&#])           //should contain at least one special char
(?=.*[A-Z])             //should contain at least one upper case
[a-zA-Z0-9]{4,}         //should contain at least 8 from the mentioned characters
$/)

Solution 4:

None of the above worked for me.

What worked for me:

fun isValidPasswordFormat(password: String): Boolean {
    val passwordREGEX = Pattern.compile("^" +
        "(?=.*[0-9])" +         //at least 1 digit
        "(?=.*[a-z])" +         //at least 1 lower case letter
        "(?=.*[A-Z])" +         //at least 1 upper case letter
        "(?=.*[a-zA-Z])" +      //any letter
        "(?=.*[@#$%^&+=])" +    //at least 1 special character
        "(?=\\S+$)" +           //no white spaces
        ".{8,}" +               //at least 8 characters
        "$");
    return passwordREGEX.matcher(password).matches()
}

Source: Coding in Flow

Hope it helps someone.

Solution 5:

try {
    if (subjectString.matches("^(?=.*[@$%&#_()=+?»«<>£§€{}\\[\\]-])(?=.*[A-Z])(?=.*[a-z])(?=.*\\d).*(?<=.{4,})$")) {
        // String matched entirely
    } else {
        // Match attempt failed
    } 
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}


(?=.*[@\$%&#_()=+?»«<>£§€{}.[\]-]) -> must have at least 1 special character
(?=.*[A-Z])   -> Must have at least 1 upper case letter
(?=.*[a-z])   -> Must have at least 1 lower case letter
(?=.*\\d)     -> Must have at least 1 digit
(?<=.{4,})$") -> Must be equal or superior to 4 chars.