RepeatCharactersRule example

In this post under Passy, I will explain with example, the purpose of “RepeatCharactersRule” class.

Sometimes user entered password will have “n” characters (i.e., number of characters) with each character being repeated equal to or more than “m” times.

For example in case of this password “11a22b333xyz”
1) n is 3 which are 1, 2, and 3.
2) m in case of n = 1 is 2, m in case of n = 2 is 2, and m in case of n = 3 is 3.

In the above example we name “m” as sequence length and “n” as sequence count.

We can have a rule which dictates max how many character can be repeated (i.e., sequence count) and max how many time each character can be repeated (i.e, sequence length).

For this purpose we use class “RepeatCharactersRule” which will help us create the above rule.

Below is the main class for your reference, showing how to use “RepeatCharactersRule” class

Main class

1  package defaultPackage;
2  
3  import java.util.Scanner;
4  
5  import org.passay.PasswordData;
6  import org.passay.PasswordValidator;
7  import org.passay.RepeatCharactersRule;
8  import org.passay.Rule;
9  import org.passay.RuleResult;
10 
11 public class Example7 {
12     public static void main(String[] args) {
13         //first parameter sequence length and second parameter is sequence count
14         Rule repeatCharactersRule = new RepeatCharactersRule(2, 3);
15         PasswordValidator passwordValidator = new PasswordValidator(repeatCharactersRule);
16         Scanner scanner = new Scanner(System.in);
17         System.out.println("First test");
18         System.out.println("Enter password");
19         String data = scanner.next();
20         PasswordData passwordData = new PasswordData(data);
21         RuleResult ruleResult = passwordValidator.validate(passwordData);
22         System.out.println("Result of password validation: " + ruleResult.isValid());
23         System.out.println("--------------------------------------");
24         
25         System.out.println("Second test");
26         System.out.println("Enter password");
27         data = scanner.next();
28         passwordData = new PasswordData(data);
29         ruleResult = passwordValidator.validate(passwordData);
30         System.out.println("Result of password validation: " + ruleResult.isValid());
31         System.out.println("--------------------------------------");
32         
33         System.out.println("Third test");
34         System.out.println("Enter password");
35         data = scanner.next();
36         passwordData = new PasswordData(data);
37         ruleResult = passwordValidator.validate(passwordData);
38         System.out.println("Result of password validation: " + ruleResult.isValid());
39     }
40 }

In the above code, at line 14, I created an instance of “RepeatCharactersRule” class where the first parameter is “sequence length” and second parameter is “sequence count”.

So we are telling Passay, if a password contains 3 characters (i.e., sequence count as 3) and each character is repeated equal to or more than 2 times (i.e., sequence length as 2) fail the validation
of the password.

In this way, we can use the “RepeatCharactersRule” class.

Below is the output for your reference

First test
Enter password
11a22b333xyz
Result of password validation: false
--------------------------------------
Second test
Enter password
11a22bxyz
Result of password validation: true
--------------------------------------
Third test
Enter password
1a2b3xyz
Result of password validation: true

Leave a comment