Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the implementation class where i've implemented a regex to check correct password:


class Implementation {
	public String passwordValidator(Credentials details) throws WeakPasswordException {

		String a = "[A-Za-z][A-Za-z0-9]+";
		String b = "[A-Za-z0-9]{8,41}";
		if (Pattern.matches(a, details.password) && Pattern.matches(b, details.password)) {
			return "valid password";
		} else if (!(Pattern.matches(a, details.password))) {
			throw new WeakPasswordException("atleast one alphabet and one digit");
		} else if (!(Pattern.matches(b, details.password))) {
			throw new WeakPasswordException("length should be between 8 and 40");
		}
		return "valid password";

	}

	public String signUp(Credentials details) {

		try {
			passwordValidator(details);
			return "successful";
		} catch (WeakPasswordException e) {
			return "invalid password!!";
		} catch (Exception e) {
			return "other";
		}
	}
}


here is the weakpasswordexception definition:

class WeakPasswordException extends Exception {

	String argument;

	public WeakPasswordException() {
	}

	public WeakPasswordException(String argument) {
		super(argument);
	}

}


But when I pass a wrong password for ex"13", it throws the WeakPasswordException but the catch block doesn't return the "invalid password!!" statement.

What I have tried:

I tried using e.getMessage() and after that returned the statement, but it doesn't help....
Posted
Updated 12-Apr-22 22:26pm
v2
Comments
Richard MacCutchan 13-Apr-22 3:30am    
Where is the definition of WeakPasswordException?
Sourabh Jambale 13-Apr-22 3:58am    
Yes, added. Thank you!
Richard MacCutchan 13-Apr-22 4:26am    
See below.

1 solution

OK, I managed to test this and your regex patterns are incorrect. Pattern a accepts any alphabetic character followed by any alphanumeric character repeated a number of times. Pattern b accepts any alphanumeric character repeated between 8 and 41 times. So they basically accept most strings that you pass in. You need to make them more restrictive, see Pattern (Java Platform SE 7 )[^].
 
Share this answer
 
Comments
Sourabh Jambale 13-Apr-22 4:50am    
But the requirement is to check if the password contains atleast one digit and one alphabet and that the password length should be between 8 and 40...
Richard MacCutchan 13-Apr-22 5:13am    
Yes, I understand that, but your patterns are not correct.
Richard MacCutchan 13-Apr-22 12:00pm    
Try this for the pattern:
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,41}$

It should cover both cases.
Sourabh Jambale 13-Apr-22 12:03pm    
But we would need to split it because we want to check the 2 cases separately, correct?
Richard MacCutchan 13-Apr-22 12:06pm    
No, it should work for both cases. The expression requires at least one alphabetic character, at least one numeric character, and a minimum of 8 and maximum of 41.

It should be simple to test with a few different sample strings.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900