Validation/Generation
- Passay - Password Validation
- Passay - Customized Messages
- Passay - M of N Rules
- Passay - Password Generation
Positive Matching Rules
- passay - AllowedCharacterRule
- Passay - AllowedRegexRule
- Passay - CharacterRule
- passay - LengthRule
- Passay - CharacterCharacteristicsRule
- Passay - LengthComplexityRule
Negative Matching Rules
- Passay - lllegalCharacterRule
- Passay - NumberRangeRule
- Passay - WhitespaceRule
- Passay - DictionaryRule
- Passay - DictionarySubstringRule
- Passay - HistoryRule
- passay - RepeatCharacterRegexRule
- Passay - UsernameRule
Passay Useful Resources
Passay - LengthRule
LengthRule class helps in defining the minimum and maximum length of a password.
Example - Validating Password as per Length Rule
The below example shows validation of an invalid password as per length rules.
PassayDemo.java
package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.passay.LengthRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.Rule;
import org.passay.RuleResult;
public class PassayDemo {
public static void main(String[] args) {
List<Rule> rules = new ArrayList<>();
//Rule 1: Password length should be in between
//8 and 16 characters
rules.add(new LengthRule(8, 16));
PasswordValidator validator = new PasswordValidator(rules);
PasswordData password = new PasswordData("Micro");
RuleResult result = validator.validate(password);
if(result.isValid()){
System.out.println("Password validated.");
}else{
System.out.println("Invalid Password: " + validator.getMessages(result));
}
}
}
Output
Compile and run the code to verify the result −
Invalid Password: [Password must be 8 or more characters in length.]
Example - Validating a Valid Password as per Length Rule
The below example shows validation of a valid password as per length rules.
PassayDemo.java
package com.tutorialspoint;
import java.util.ArrayList;
import java.util.List;
import org.passay.LengthRule;
import org.passay.PasswordData;
import org.passay.PasswordValidator;
import org.passay.Rule;
import org.passay.RuleResult;
public class PassayDemo {
public static void main(String[] args) {
List<Rule> rules = new ArrayList<>();
//Rule 1: Password length should be in between
//8 and 16 characters
rules.add(new LengthRule(8, 16));
PasswordValidator validator = new PasswordValidator(rules);
PasswordData password = new PasswordData("Microsoft");
RuleResult result = validator.validate(password);
if(result.isValid()){
System.out.println("Password validated.");
}else{
System.out.println("Invalid Password: " + validator.getMessages(result));
}
}
}
Output
Compile and run the code to verify the result −
Password validated.
Advertisements