IllegalSequenceRule example

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

This class is used to verify that the user given password doesn’t contain known illegal sequence of characters like “qwerty” etc.

Below is the complete code showing how to use it

Main class

package defaultPackage;

import org.passay.EnglishSequenceData;
import org.passay.IllegalSequenceRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.RuleResult;

public class Example22 {
    public static void main(String[] args) {
        IllegalSequenceRule illegalSequenceRule = new IllegalSequenceRule(EnglishSequenceData.USQwerty);
        PasswordValidator passwordValidator = new PasswordValidator(illegalSequenceRule);

        PasswordData passwordData = new PasswordData("aabb");
        RuleResult ruleResult = passwordValidator.validate(passwordData);
        System.out.println(ruleResult.isValid());

        passwordData = new PasswordData("qwerty");
        ruleResult = passwordValidator.validate(passwordData);
        System.out.println(ruleResult.isValid());

        passwordData = new PasswordData("sam_qwerty_@");
        ruleResult = passwordValidator.validate(passwordData);
        System.out.println(ruleResult.isValid());
    }
}

In the above code, at line 11, I create an instance of “IllegalSequenceRule” class and pass existing, out of the box provided USA querty sequence instance.

At line 12, I create an instance of “PasswordValidator” passing the instance of “IllegalSequenceRule” created at line 11.

Next we take three inputs and validate it.

In the first case, refer to line 14, the validation passes. As it doesn’t contain the querty sequence.

Whereas as validation fails for the second and third case. As it contains the querty sequence.

In this way we can use “IllegalSequenceRule” class.

Leave a comment