In this post under Passay, I will explain with example the purpose of “RepeatCharacterRegexRule” rule.
This rule checks the user inputed password for repeated ASCII characters. If a particular ASCII character is repeated more than the preconfigured character count, the rule will invalidate the password.
Let’s say, the pre-configured character count is 3, and below are the password we are validating
- a_bbb_cc
- a_bb_cc
- a_bbbb_cc
The password “a_bb_cc” will pass the validation whereas the other two will fail because the character ‘b’ is repeated more than 3 times.
Below is the main class showing how to use the “RepeatCharacterRegexRule” class
Main class
package defaultPackage;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.RepeatCharacterRegexRule;
import org.passay.RuleResult;
public class Example26 {
public static void main(String[] args) {
RepeatCharacterRegexRule repeatCharacterRegexRule = new RepeatCharacterRegexRule();
PasswordValidator passwordValidator = new PasswordValidator(repeatCharacterRegexRule);
PasswordData passwordData = new PasswordData("aa_bbb_ccdefgh");
RuleResult ruleResult = passwordValidator.validate(passwordData);
System.out.println("is aa_bbb_ccdefgh valid: " + ruleResult.isValid());
passwordData = new PasswordData("aa_bbbbb_ccdefgh");
ruleResult = passwordValidator.validate(passwordData);
System.out.println("is aa_bbbbb_ccdefgh valid: " + ruleResult.isValid());
passwordData = new PasswordData("aa_bb_ccdefgh");
ruleResult = passwordValidator.validate(passwordData);
System.out.println("is aa_bb_ccdefgh valid: " + ruleResult.isValid());
}
}
In the above code, at line 10, I create an instance of “RepeatCharacterRegexRule” using no-argument constructor. As a result the default character count of 5 will be used.
At line 11, I create an instance of “PasswordValidator” using the newly created instance of “RepeatCharacterRegexRule” as an argument.
At line 14, I validate the password “aa_bbb_ccdefgh” and the result is printed at line 15.
At line 18, I validate the password “aa_bbbbb_ccdefgh” and the result is printed at line 19
At line 22, I validate the password “aa_bb_ccdefgh” and the result is printed at line 23
In this way, we can use “RepeatCharacterRegexRule”
Below is the output
Output
is aa_bbb_ccdefgh valid: true
is aa_bbbbb_ccdefgh valid: false
is aa_bb_ccdefgh valid: true