lengthcomplexityrule helps in defining the applicable rule on a password based on its length. consider the following policy.
if length of password is in between 1 to 5 characters, only lower case alphabets are allowed.
if length of password is in between 6 to 8 characters, then only a, b and c are allowed.
example
the below example shows the validation of a password against above policy using passay library.
import org.passay.allowedcharacterrule;
import org.passay.characterrule;
import org.passay.englishcharacterdata;
import org.passay.lengthcomplexityrule;
import org.passay.passworddata;
import org.passay.passwordvalidator;
import org.passay.ruleresult;
public class passayexample {
public static void main(string[] args) {
lengthcomplexityrule lengthcomplexityrule = new lengthcomplexityrule();
//rule: password of 1 to 5 characters should contains lower case alphabets only
lengthcomplexityrule.addrules("[1,5]",
new characterrule(englishcharacterdata.lowercase, 5));
//8 and 16 characters
lengthcomplexityrule.addrules("[6,8]",
new allowedcharacterrule(new char[] { 'a', 'b', 'c' }));
passwordvalidator validator = new passwordvalidator(lengthcomplexityrule);
passworddata password = new passworddata("abcdef");
ruleresult result = validator.validate(password);
if(result.isvalid()){
system.out.println("password validated.");
}else{
system.out.println("invalid password: " + validator.getmessages(result));
}
}
}
output
invalid password: [ password contains the illegal character 'd'., password contains the illegal character 'e'., password contains the illegal character 'f'., password meets 0 complexity rules, but 1 are required.]