In this post under Passay, I will explain with example the purpose of “CharacterOccurrencesRule” rule class.
They are cases when user enters same character multiple times when comming up with their own password.
If we want prevent such scenarios or if you want to keep a contol on how many times a particular character is repeated, we can take help of “CharacterOccurrencesRule” rule class.
The “CharacterOccurrencesRule” rule class makes sure that if any character in the password is repeated, it doesn’t get repeated beyond the maximum allowed.
This class constructor takes a integer argument which represents the maximum number of times any character can be repeated.
This “CharacterOccurrencesRule” rule can also be used for generating password. When generating password the rule will make sure that the password generated by “PasswordGenerator” class doesn’t
repeat a character more than the allowed maximum.
Below is the main class that shows how to use it.
Main class
1 package defaultPackage;
2
3 import java.util.Scanner;
4
5 import org.passay.CharacterOccurrencesRule;
6 import org.passay.PasswordData;
7 import org.passay.PasswordValidator;
8 import org.passay.Rule;
9 import org.passay.RuleResult;
10
11 public class Example8 {
12 public static void main(String[] args) {
13 Rule characterOccurrencesRule = new CharacterOccurrencesRule(3);
14 PasswordValidator passwordValidator = new PasswordValidator(characterOccurrencesRule);
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 System.out.println("--------------------------------------");
39 }
40 }
In the above code, at line 13, I create an instance of “CharacterOccurrencesRule” class and passing 3 as constructor argument. So now this rule will make sure that if a character is repeated it doesn’t get repeated more than 3 times.
At line 14, I pass the instance created at line 13, as a constructor argument when creating an instance of PasswordValidator.
Then I am asking user input 3 times and check there validity by calling “isValid” method on “passwordValidator” instance.
Below is the output
Output
First test
Enter password
hello
Result of password validation: true
--------------------------------------
Second test
Enter password
helllo
Result of password validation: true
--------------------------------------
Third test
Enter password
hellllo
Result of password validation: false
--------------------------------------