IllegalSequenceRule with custom character sequences example 2

In previous post under Passay, I showed one example of how to use “IllegalSequenceRule” with custom character sequences.

This post is continuation of the previous post with another example.

In the previous post, I used different two custom character sequences, one character sequence containing only numbers and another character sequence containing only alphabets.

In this post I will use two custom character sequences, both having numbers.

Below is the enum “NumberedCustomSequenceData” structure

NumberedCustomSequenceData

enum NumberedCustomSequenceData implements SequenceData {
    CUSTOM_SEQUENCE_1("CUSTOM_SEQUENCE_1", new CharacterSequence[] {new CharacterSequence("12345"), new CharacterSequence("56789")});

    private String errorCode;
    private CharacterSequence[] characterSequences;

    NumberedCustomSequenceData(String errorCode, CharacterSequence[] characterSequences) {
        this.errorCode = errorCode;
        this.characterSequences = characterSequences;
    }

    @Override
    public String getErrorCode() {
        return this.errorCode;
    }

    @Override
    public CharacterSequence[] getSequences() {
        return this.characterSequences;
    }
}

Next I will show how to use it with “IllegalSequenceRule” in main class

Main class

public class Example25 {
    public static void main(String[] args) {
        IllegalSequenceRule illegalSequenceRule = new IllegalSequenceRule(NumberedCustomSequenceData.CUSTOM_SEQUENCE_1);
        PasswordValidator passwordValidator = new PasswordValidator(illegalSequenceRule);

        PasswordData passwordData = new PasswordData("aabb");
        RuleResult ruleResult = passwordValidator.validate(passwordData);
        System.out.println("Validation of 'aabb': " + ruleResult.isValid());

        passwordData = new PasswordData("12345");
        ruleResult = passwordValidator.validate(passwordData);
        System.out.println("Validation of '12345': " + ruleResult.isValid());

        passwordData = new PasswordData("sam_12345_@");
        ruleResult = passwordValidator.validate(passwordData);
        System.out.println("Validation of 'sam_12345_@': " + ruleResult.isValid());

        passwordData = new PasswordData("56789");
        ruleResult = passwordValidator.validate(passwordData);
        System.out.println("Validation of '56789': " + ruleResult.isValid());

        passwordData = new PasswordData("sam_56789_@");
        ruleResult = passwordValidator.validate(passwordData);
        System.out.println("Validation of 'sam_56789_@': " + ruleResult.isValid());
    }
}

Below is the output

Output

Validation of 'aabb': true
Validation of '12345': false
Validation of 'sam_12345_@': false
Validation of '56789': false
Validation of 'sam_56789_@': false

Leave a comment