In this post under Passay, I will show with example how to verify whether a user entered password is within the configured length.
Below is the complete main code for your reference.
Main code
1 package defaultPackage;
2
3 import java.util.Scanner;
4
5 import org.passay.LengthRule;
6 import org.passay.PasswordData;
7 import org.passay.PasswordValidator;
8 import org.passay.Rule;
9 import org.passay.RuleResult;
10
11 public class Example1 {
12 public static void main(String[] args) {
13 Rule lengthRule = new LengthRule(8, 16);
14 PasswordValidator passwordValidator = new PasswordValidator(lengthRule);
15 Scanner scanner = new Scanner(System.in);
16 System.out.println("First test");
17 System.out.println("Enter password");
18 String data = scanner.next();
19 PasswordData passwordData = new PasswordData(data);
20 RuleResult ruleResult = passwordValidator.validate(passwordData);
21 System.out.println("Result of password validation: " + ruleResult.isValid());
22 System.out.println("--------------------------------------");
23
24 System.out.println("Second test");
25 System.out.println("Enter password");
26 data = scanner.next();
27 passwordData = new PasswordData(data);
28 ruleResult = passwordValidator.validate(passwordData);
29 System.out.println("Result of password validation: " + ruleResult.isValid());
30 System.out.println("--------------------------------------");
31
32 System.out.println("Third test");
33 System.out.println("Enter password");
34 data = scanner.next();
35 passwordData = new PasswordData(data);
36 ruleResult = passwordValidator.validate(passwordData);
37 System.out.println("Result of password validation: " + ruleResult.isValid());
38 }
39 }
In the above code, at line 13 I create a rule which says the length of the password should between 8 and 16. So minimum length should 8 and maximum length should 16.
Anything less than 8 is invalid and anything more than 16 is invalid. Whereas within 8 and 16 is a valid password.
To create this rule, I take help of “Rule” inteface and one of its implementations called “LengthRule”. Both are provided by Passay framework for use.
At line line 14, I create an instance of “PasswordValidator” class and pass the rule as an constructor argument.
This will create a “PasswordValidator” instance by name “passwordValidator” which will validate the length rule.
Next I perform the first test (refer line 16 to 22), in which I take user input and assign it to “data” String variable. Refer to line 18.
At line 19, I create an instance “PasswordData” class and pass the String input (i.e., data variable) as a constructor argument.
This “PasswordData” class will have password related information. It will be used by rules to perform validation.
At line 20, I call “validate” method available on “passwordValidator” instance and pass “PasswordData” instance as an argument.
The “validate” method return an instance “RuleResult” which will contain test results.
At line 21, I check whether user entered password is valid or not by calling “isValid” method on “RuleResult” instance.
In this way we can validate whether a password is within the given length.
Below is the output for your reference
Output
First test
Enter password
hello
Result of password validation: false
--------------------------------------
Second test
Enter password
hellohello
Result of password validation: true
--------------------------------------
Third test
Enter password
hellohellohellohello
Result of password validation: false